widl: Follow aliases in check_remoting_fields so that the fields for the type are...
[wine] / tools / widl / parser.y
1 %{
2 /*
3  * IDL Compiler
4  *
5  * Copyright 2002 Ove Kaaven
6  * Copyright 2006-2008 Robert Shearman
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
31 #ifdef HAVE_ALLOCA_H
32 #include <alloca.h>
33 #endif
34
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typelib.h"
40 #include "typegen.h"
41 #include "expr.h"
42
43 #if defined(YYBYACC)
44         /* Berkeley yacc (byacc) doesn't seem to know about these */
45         /* Some *BSD supplied versions do define these though */
46 # ifndef YYEMPTY
47 #  define YYEMPTY       (-1)    /* Empty lookahead value of yychar */
48 # endif
49 # ifndef YYLEX
50 #  define YYLEX         yylex()
51 # endif
52
53 #elif defined(YYBISON)
54         /* Bison was used for original development */
55         /* #define YYEMPTY -2 */
56         /* #define YYLEX   yylex() */
57
58 #else
59         /* No yacc we know yet */
60 # if !defined(YYEMPTY) || !defined(YYLEX)
61 #  error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
62 # elif defined(__GNUC__)        /* gcc defines the #warning directive */
63 #  warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
64   /* #else we just take a chance that it works... */
65 # endif
66 #endif
67
68 #define YYERROR_VERBOSE
69
70 unsigned char pointer_default = RPC_FC_UP;
71 static int is_in_interface = FALSE;
72 static int is_object_interface = FALSE;
73 /* are we inside a library block? */
74 static int is_inside_library = FALSE;
75
76 typedef struct list typelist_t;
77 struct typenode {
78   type_t *type;
79   struct list entry;
80 };
81
82 struct _import_t
83 {
84   char *name;
85   int import_performed;
86 };
87
88 typedef struct _decl_spec_t
89 {
90   type_t *type;
91   attr_list_t *attrs;
92   enum storage_class stgclass;
93 } decl_spec_t;
94
95 typelist_t incomplete_types = LIST_INIT(incomplete_types);
96
97 static void add_incomplete(type_t *t);
98 static void fix_incomplete(void);
99
100 static str_list_t *append_str(str_list_t *list, char *str);
101 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
102 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
103 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass);
104 static attr_t *make_attr(enum attr_type type);
105 static attr_t *make_attrv(enum attr_type type, unsigned long val);
106 static attr_t *make_attrp(enum attr_type type, void *val);
107 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
108 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
109 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl, int top);
110 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
111 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
112 static ifref_t *make_ifref(type_t *iface);
113 static var_list_t *append_var(var_list_t *list, var_t *var);
114 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
115 static var_t *make_var(char *name);
116 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
117 static declarator_t *make_declarator(var_t *var);
118 static func_list_t *append_func(func_list_t *list, func_t *func);
119 static func_t *make_func(var_t *def);
120 static type_t *make_class(char *name);
121 static type_t *make_safearray(type_t *type);
122 static type_t *make_builtin(char *name);
123 static type_t *make_int(int sign);
124 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
125 static type_t *make_func_type(var_list_t *args);
126 static type_t *make_pointer_type(type_t *ref, attr_list_t *attrs);
127 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
128
129 static type_t *reg_type(type_t *type, const char *name, int t);
130 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
131 static type_t *find_type_or_error(const char *name, int t);
132 static type_t *find_type_or_error2(char *name, int t);
133 static type_t *get_type(unsigned char type, char *name, int t);
134 static type_t *get_typev(unsigned char type, var_t *name, int t);
135
136 static var_t *reg_const(var_t *var);
137
138 static void write_libid(const typelib_t *typelib);
139 static void write_clsid(type_t *cls);
140 static void write_diid(type_t *iface);
141 static void write_iid(type_t *iface);
142
143 static int compute_method_indexes(type_t *iface);
144 static char *gen_name(void);
145 static statement_t *process_typedefs(var_list_t *names);
146 static void check_arg(var_t *arg);
147 static void check_functions(const type_t *iface);
148 static void check_all_user_types(const statement_list_t *stmts);
149 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
150 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
151 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
152 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
153 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
154 static attr_list_t *check_union_attrs(attr_list_t *attrs);
155 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
156 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
157 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
158 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
159 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
160 const char *get_attr_display_name(enum attr_type type);
161 static void add_explicit_handle_if_necessary(func_t *func);
162 static void check_def(const type_t *t);
163
164 static statement_t *make_statement(enum statement_type type);
165 static statement_t *make_statement_type_decl(type_t *type);
166 static statement_t *make_statement_reference(type_t *type);
167 static statement_t *make_statement_declaration(var_t *var);
168 static statement_t *make_statement_library(typelib_t *typelib);
169 static statement_t *make_statement_cppquote(const char *str);
170 static statement_t *make_statement_importlib(const char *str);
171 static statement_t *make_statement_module(type_t *type);
172 static statement_t *make_statement_import(const char *str);
173 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
174 static func_list_t *append_func_from_statement(func_list_t *list, statement_t *stmt);
175
176 #define tsENUM   1
177 #define tsSTRUCT 2
178 #define tsUNION  3
179
180 %}
181 %union {
182         attr_t *attr;
183         attr_list_t *attr_list;
184         str_list_t *str_list;
185         expr_t *expr;
186         expr_list_t *expr_list;
187         array_dims_t *array_dims;
188         type_t *type;
189         var_t *var;
190         var_list_t *var_list;
191         declarator_t *declarator;
192         declarator_list_t *declarator_list;
193         func_t *func;
194         func_list_t *func_list;
195         statement_t *statement;
196         statement_list_t *stmt_list;
197         ifref_t *ifref;
198         ifref_list_t *ifref_list;
199         char *str;
200         UUID *uuid;
201         unsigned int num;
202         double dbl;
203         interface_info_t ifinfo;
204         typelib_t *typelib;
205         struct _import_t *import;
206         struct _decl_spec_t *declspec;
207         enum storage_class stgclass;
208 }
209
210 %token <str> aIDENTIFIER
211 %token <str> aKNOWNTYPE
212 %token <num> aNUM aHEXNUM
213 %token <dbl> aDOUBLE
214 %token <str> aSTRING aWSTRING
215 %token <uuid> aUUID
216 %token aEOF
217 %token SHL SHR
218 %token MEMBERPTR
219 %token EQUALITY INEQUALITY
220 %token GREATEREQUAL LESSEQUAL
221 %token LOGICALOR LOGICALAND
222 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
223 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
224 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
225 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
226 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
227 %token tDEFAULT
228 %token tDEFAULTCOLLELEM
229 %token tDEFAULTVALUE
230 %token tDEFAULTVTABLE
231 %token tDISPLAYBIND
232 %token tDISPINTERFACE
233 %token tDLLNAME tDOUBLE tDUAL
234 %token tENDPOINT
235 %token tENTRY tENUM tERRORSTATUST
236 %token tEXPLICITHANDLE tEXTERN
237 %token tFALSE
238 %token tFASTCALL
239 %token tFLOAT
240 %token tHANDLE
241 %token tHANDLET
242 %token tHELPCONTEXT tHELPFILE
243 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
244 %token tHIDDEN
245 %token tHYPER tID tIDEMPOTENT
246 %token tIIDIS
247 %token tIMMEDIATEBIND
248 %token tIMPLICITHANDLE
249 %token tIMPORT tIMPORTLIB
250 %token tIN tIN_LINE tINLINE
251 %token tINPUTSYNC
252 %token tINT tINT64
253 %token tINTERFACE
254 %token tLCID
255 %token tLENGTHIS tLIBRARY
256 %token tLOCAL
257 %token tLONG
258 %token tMETHODS
259 %token tMODULE
260 %token tNONBROWSABLE
261 %token tNONCREATABLE
262 %token tNONEXTENSIBLE
263 %token tNULL
264 %token tOBJECT tODL tOLEAUTOMATION
265 %token tOPTIONAL
266 %token tOUT
267 %token tPASCAL
268 %token tPOINTERDEFAULT
269 %token tPROPERTIES
270 %token tPROPGET tPROPPUT tPROPPUTREF
271 %token tPTR
272 %token tPUBLIC
273 %token tRANGE
274 %token tREADONLY tREF
275 %token tREGISTER
276 %token tREQUESTEDIT
277 %token tRESTRICTED
278 %token tRETVAL
279 %token tSAFEARRAY
280 %token tSHORT
281 %token tSIGNED
282 %token tSINGLE
283 %token tSIZEIS tSIZEOF
284 %token tSMALL
285 %token tSOURCE
286 %token tSTATIC
287 %token tSTDCALL
288 %token tSTRICTCONTEXTHANDLE
289 %token tSTRING tSTRUCT
290 %token tSWITCH tSWITCHIS tSWITCHTYPE
291 %token tTRANSMITAS
292 %token tTRUE
293 %token tTYPEDEF
294 %token tUNION
295 %token tUNIQUE
296 %token tUNSIGNED
297 %token tUUID
298 %token tV1ENUM
299 %token tVARARG
300 %token tVERSION
301 %token tVOID
302 %token tWCHAR tWIREMARSHAL
303
304 %type <attr> attribute type_qualifier function_specifier
305 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
306 %type <str_list> str_list
307 %type <expr> m_expr expr expr_const expr_int_const array
308 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
309 %type <ifinfo> interfacehdr
310 %type <stgclass> storage_cls_spec
311 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
312 %type <type> inherit interface interfacedef interfacedec
313 %type <type> dispinterface dispinterfacehdr dispinterfacedef
314 %type <type> module modulehdr moduledef
315 %type <type> base_type int_std
316 %type <type> enumdef structdef uniondef typedecl
317 %type <type> type
318 %type <ifref> coclass_int
319 %type <ifref_list> coclass_ints
320 %type <var> arg ne_union_field union_field s_field case enum declaration
321 %type <var_list> m_args no_args args fields ne_union_fields cases enums enum_list dispint_props field
322 %type <var> m_ident t_ident ident
323 %type <declarator> declarator direct_declarator init_declarator
324 %type <declarator_list> declarator_list
325 %type <func> funcdef
326 %type <func_list> int_statements dispint_meths
327 %type <type> coclass coclasshdr coclassdef
328 %type <num> pointer_type version
329 %type <str> libraryhdr callconv cppquote importlib import
330 %type <uuid> uuid_string
331 %type <import> import_start
332 %type <typelib> library_start librarydef
333 %type <statement> statement typedef
334 %type <stmt_list> gbl_statements imp_statements
335
336 %left ','
337 %right '?' ':'
338 %left LOGICALOR
339 %left LOGICALAND
340 %left '|'
341 %left '^'
342 %left '&'
343 %left EQUALITY INEQUALITY
344 %left '<' '>' LESSEQUAL GREATEREQUAL
345 %left SHL SHR
346 %left '-' '+'
347 %left '*' '/' '%'
348 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
349 %left '.' MEMBERPTR '[' ']'
350
351 %%
352
353 input:   gbl_statements                         { fix_incomplete();
354                                                   check_all_user_types($1);
355                                                   write_proxies($1);
356                                                   write_client($1);
357                                                   write_server($1);
358                                                   write_dlldata($1);
359                                                 }
360         ;
361
362 gbl_statements:                                 { $$ = NULL; }
363         | gbl_statements interfacedec           { $$ = $1; }
364         | gbl_statements interfacedef           { $$ = append_statement($1, make_statement_type_decl($2)); }
365         | gbl_statements coclass ';'            { $$ = $1;
366                                                   reg_type($2, $2->name, 0);
367                                                   if (!parse_only && do_header) write_coclass_forward($2);
368                                                 }
369         | gbl_statements coclassdef             { $$ = append_statement($1, make_statement_type_decl($2));
370                                                   add_typelib_entry($2);
371                                                   reg_type($2, $2->name, 0);
372                                                   if (!parse_only && do_header) write_coclass_forward($2);
373                                                 }
374         | gbl_statements moduledef              { $$ = append_statement($1, make_statement_module($2));
375                                                   add_typelib_entry($2);
376                                                 }
377         | gbl_statements librarydef             { $$ = append_statement($1, make_statement_library($2)); }
378         | gbl_statements statement              { $$ = append_statement($1, $2); }
379         ;
380
381 imp_statements:                                 { $$ = NULL; }
382         | imp_statements interfacedec           { $$ = append_statement($1, make_statement_reference($2)); if (!parse_only) add_typelib_entry($2); }
383         | imp_statements interfacedef           { $$ = append_statement($1, make_statement_type_decl($2)); if (!parse_only) add_typelib_entry($2); }
384         | imp_statements coclass ';'            { $$ = $1; reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
385         | imp_statements coclassdef             { $$ = append_statement($1, make_statement_type_decl($2));
386                                                   if (!parse_only) add_typelib_entry($2);
387                                                   reg_type($2, $2->name, 0);
388                                                   if (!parse_only && do_header) write_coclass_forward($2);
389                                                 }
390         | imp_statements moduledef              { $$ = append_statement($1, make_statement_module($2)); if (!parse_only) add_typelib_entry($2); }
391         | imp_statements statement              { $$ = append_statement($1, $2); }
392         | imp_statements importlib              { $$ = append_statement($1, make_statement_importlib($2)); }
393         | imp_statements librarydef             { $$ = append_statement($1, make_statement_library($2)); }
394         ;
395
396 int_statements:                                 { $$ = NULL; }
397         | int_statements statement              { $$ = append_func_from_statement( $1, $2 ); }
398         ;
399
400 semicolon_opt:
401         | ';'
402         ;
403
404 statement:
405           cppquote                              { $$ = make_statement_cppquote($1); }
406         | typedecl ';'                          { $$ = make_statement_type_decl($1);
407                                                   if (!parse_only && do_header) {
408                                                     write_type_def_or_decl(header, $1, FALSE, NULL);
409                                                     fprintf(header, ";\n\n");
410                                                   }
411                                                 }
412         | declaration ';'                       { $$ = make_statement_declaration($1);
413                                                   if (!parse_only && do_header) write_declaration($1, is_in_interface);
414                                                 }
415         | import                                { $$ = make_statement_import($1); }
416         | typedef ';'                           { $$ = $1; }
417         ;
418
419 typedecl:
420           enumdef
421         | structdef
422         | uniondef
423         | attributes enumdef                    { $$ = $2; $$->attrs = check_enum_attrs($1); }
424         | attributes structdef                  { $$ = $2; $$->attrs = check_struct_attrs($1); }
425         | attributes uniondef                   { $$ = $2; $$->attrs = check_union_attrs($1); }
426         ;
427
428 cppquote: tCPPQUOTE '(' aSTRING ')'             { $$ = $3; if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
429         ;
430 import_start: tIMPORT aSTRING ';'               { assert(yychar == YYEMPTY);
431                                                   $$ = xmalloc(sizeof(struct _import_t));
432                                                   $$->name = $2;
433                                                   $$->import_performed = do_import($2);
434                                                   if (!$$->import_performed) yychar = aEOF;
435                                                 }
436         ;
437
438 import: import_start imp_statements aEOF        { $$ = $1->name;
439                                                   if ($1->import_performed) pop_import();
440                                                   free($1);
441                                                   if (!parse_only && do_header) write_import($$);
442                                                 }
443         ;
444
445 importlib: tIMPORTLIB '(' aSTRING ')'
446            semicolon_opt                        { $$ = $3; if(!parse_only) add_importlib($3); }
447         ;
448
449 libraryhdr: tLIBRARY aIDENTIFIER                { $$ = $2; }
450         ;
451 library_start: attributes libraryhdr '{'        { $$ = make_library($2, check_library_attrs($2, $1));
452                                                   if (!parse_only) start_typelib($$);
453                                                   if (!parse_only && do_header) write_library($$);
454                                                   if (!parse_only && do_idfile) write_libid($$);
455                                                   is_inside_library = TRUE;
456                                                 }
457         ;
458 librarydef: library_start imp_statements '}'
459             semicolon_opt                       { $$ = $1;
460                                                   $$->stmts = $2;
461                                                   if (!parse_only) end_typelib();
462                                                   is_inside_library = FALSE;
463                                                 }
464         ;
465
466 m_args:                                         { $$ = NULL; }
467         | args
468         ;
469
470 no_args:  tVOID                                 { $$ = NULL; }
471         ;
472
473 args:     arg                                   { check_arg($1); $$ = append_var( NULL, $1 ); }
474         | args ',' arg                          { check_arg($3); $$ = append_var( $1, $3); }
475         | no_args
476         ;
477
478 /* split into two rules to get bison to resolve a tVOID conflict */
479 arg:      attributes decl_spec declarator       { $$ = $3->var;
480                                                   $$->attrs = $1;
481                                                   if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
482                                                     error_loc("invalid storage class for function parameter\n");
483                                                   set_type($$, $2, $3, TRUE);
484                                                   free($3);
485                                                 }
486         | decl_spec declarator                  { $$ = $2->var;
487                                                   if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
488                                                     error_loc("invalid storage class for function parameter\n");
489                                                   set_type($$, $1, $2, TRUE);
490                                                   free($2);
491                                                 }
492         ;
493
494 array:    '[' m_expr ']'                        { $$ = $2; }
495         | '[' '*' ']'                           { $$ = make_expr(EXPR_VOID); }
496         ;
497
498 m_attributes:                                   { $$ = NULL; }
499         | attributes
500         ;
501
502 attributes:
503           '[' attrib_list ']'                   { $$ = $2; }
504         ;
505
506 attrib_list: attribute                          { $$ = append_attr( NULL, $1 ); }
507         | attrib_list ',' attribute             { $$ = append_attr( $1, $3 ); }
508         | attrib_list ']' '[' attribute         { $$ = append_attr( $1, $4 ); }
509         ;
510
511 str_list: aSTRING                               { $$ = append_str( NULL, $1 ); }
512         | str_list ',' aSTRING                  { $$ = append_str( $1, $3 ); }
513         ;
514
515 attribute:                                      { $$ = NULL; }
516         | tAGGREGATABLE                         { $$ = make_attr(ATTR_AGGREGATABLE); }
517         | tAPPOBJECT                            { $$ = make_attr(ATTR_APPOBJECT); }
518         | tASYNC                                { $$ = make_attr(ATTR_ASYNC); }
519         | tAUTOHANDLE                           { $$ = make_attr(ATTR_AUTO_HANDLE); }
520         | tBINDABLE                             { $$ = make_attr(ATTR_BINDABLE); }
521         | tBROADCAST                            { $$ = make_attr(ATTR_BROADCAST); }
522         | tCALLAS '(' ident ')'                 { $$ = make_attrp(ATTR_CALLAS, $3); }
523         | tCASE '(' expr_list_int_const ')'     { $$ = make_attrp(ATTR_CASE, $3); }
524         | tCONTEXTHANDLE                        { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
525         | tCONTEXTHANDLENOSERIALIZE             { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
526         | tCONTEXTHANDLESERIALIZE               { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
527         | tCONTROL                              { $$ = make_attr(ATTR_CONTROL); }
528         | tDEFAULT                              { $$ = make_attr(ATTR_DEFAULT); }
529         | tDEFAULTCOLLELEM                      { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
530         | tDEFAULTVALUE '(' expr_const ')'      { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
531         | tDEFAULTVTABLE                        { $$ = make_attr(ATTR_DEFAULTVTABLE); }
532         | tDISPLAYBIND                          { $$ = make_attr(ATTR_DISPLAYBIND); }
533         | tDLLNAME '(' aSTRING ')'              { $$ = make_attrp(ATTR_DLLNAME, $3); }
534         | tDUAL                                 { $$ = make_attr(ATTR_DUAL); }
535         | tENDPOINT '(' str_list ')'            { $$ = make_attrp(ATTR_ENDPOINT, $3); }
536         | tENTRY '(' expr_const ')'             { $$ = make_attrp(ATTR_ENTRY, $3); }
537         | tEXPLICITHANDLE                       { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
538         | tHANDLE                               { $$ = make_attr(ATTR_HANDLE); }
539         | tHELPCONTEXT '(' expr_int_const ')'   { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
540         | tHELPFILE '(' aSTRING ')'             { $$ = make_attrp(ATTR_HELPFILE, $3); }
541         | tHELPSTRING '(' aSTRING ')'           { $$ = make_attrp(ATTR_HELPSTRING, $3); }
542         | tHELPSTRINGCONTEXT '(' expr_int_const ')'     { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
543         | tHELPSTRINGDLL '(' aSTRING ')'        { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
544         | tHIDDEN                               { $$ = make_attr(ATTR_HIDDEN); }
545         | tID '(' expr_int_const ')'            { $$ = make_attrp(ATTR_ID, $3); }
546         | tIDEMPOTENT                           { $$ = make_attr(ATTR_IDEMPOTENT); }
547         | tIIDIS '(' expr ')'                   { $$ = make_attrp(ATTR_IIDIS, $3); }
548         | tIMMEDIATEBIND                        { $$ = make_attr(ATTR_IMMEDIATEBIND); }
549         | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')'  { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
550         | tIN                                   { $$ = make_attr(ATTR_IN); }
551         | tINPUTSYNC                            { $$ = make_attr(ATTR_INPUTSYNC); }
552         | tLENGTHIS '(' m_exprs ')'             { $$ = make_attrp(ATTR_LENGTHIS, $3); }
553         | tLCID '(' expr_int_const ')'          { $$ = make_attrp(ATTR_LIBLCID, $3); }
554         | tLOCAL                                { $$ = make_attr(ATTR_LOCAL); }
555         | tNONBROWSABLE                         { $$ = make_attr(ATTR_NONBROWSABLE); }
556         | tNONCREATABLE                         { $$ = make_attr(ATTR_NONCREATABLE); }
557         | tNONEXTENSIBLE                        { $$ = make_attr(ATTR_NONEXTENSIBLE); }
558         | tOBJECT                               { $$ = make_attr(ATTR_OBJECT); }
559         | tODL                                  { $$ = make_attr(ATTR_ODL); }
560         | tOLEAUTOMATION                        { $$ = make_attr(ATTR_OLEAUTOMATION); }
561         | tOPTIONAL                             { $$ = make_attr(ATTR_OPTIONAL); }
562         | tOUT                                  { $$ = make_attr(ATTR_OUT); }
563         | tPOINTERDEFAULT '(' pointer_type ')'  { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
564         | tPROPGET                              { $$ = make_attr(ATTR_PROPGET); }
565         | tPROPPUT                              { $$ = make_attr(ATTR_PROPPUT); }
566         | tPROPPUTREF                           { $$ = make_attr(ATTR_PROPPUTREF); }
567         | tPUBLIC                               { $$ = make_attr(ATTR_PUBLIC); }
568         | tRANGE '(' expr_int_const ',' expr_int_const ')'
569                                                 { expr_list_t *list = append_expr( NULL, $3 );
570                                                   list = append_expr( list, $5 );
571                                                   $$ = make_attrp(ATTR_RANGE, list); }
572         | tREADONLY                             { $$ = make_attr(ATTR_READONLY); }
573         | tREQUESTEDIT                          { $$ = make_attr(ATTR_REQUESTEDIT); }
574         | tRESTRICTED                           { $$ = make_attr(ATTR_RESTRICTED); }
575         | tRETVAL                               { $$ = make_attr(ATTR_RETVAL); }
576         | tSIZEIS '(' m_exprs ')'               { $$ = make_attrp(ATTR_SIZEIS, $3); }
577         | tSOURCE                               { $$ = make_attr(ATTR_SOURCE); }
578         | tSTRICTCONTEXTHANDLE                  { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
579         | tSTRING                               { $$ = make_attr(ATTR_STRING); }
580         | tSWITCHIS '(' expr ')'                { $$ = make_attrp(ATTR_SWITCHIS, $3); }
581         | tSWITCHTYPE '(' type ')'              { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
582         | tTRANSMITAS '(' type ')'              { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
583         | tUUID '(' uuid_string ')'             { $$ = make_attrp(ATTR_UUID, $3); }
584         | tV1ENUM                               { $$ = make_attr(ATTR_V1ENUM); }
585         | tVARARG                               { $$ = make_attr(ATTR_VARARG); }
586         | tVERSION '(' version ')'              { $$ = make_attrv(ATTR_VERSION, $3); }
587         | tWIREMARSHAL '(' type ')'             { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
588         | pointer_type                          { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
589         ;
590
591 uuid_string:
592           aUUID
593         | aSTRING                               { if (!is_valid_uuid($1))
594                                                     error_loc("invalid UUID: %s\n", $1);
595                                                   $$ = parse_uuid($1); }
596         ;
597
598 callconv: tCDECL                                { $$ = $<str>1; }
599         | tFASTCALL                             { $$ = $<str>1; }
600         | tPASCAL                               { $$ = $<str>1; }
601         | tSTDCALL                              { $$ = $<str>1; }
602         ;
603
604 cases:                                          { $$ = NULL; }
605         | cases case                            { $$ = append_var( $1, $2 ); }
606         ;
607
608 case:     tCASE expr_int_const ':' union_field  { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
609                                                   $$ = $4; if (!$$) $$ = make_var(NULL);
610                                                   $$->attrs = append_attr( $$->attrs, a );
611                                                 }
612         | tDEFAULT ':' union_field              { attr_t *a = make_attr(ATTR_DEFAULT);
613                                                   $$ = $3; if (!$$) $$ = make_var(NULL);
614                                                   $$->attrs = append_attr( $$->attrs, a );
615                                                 }
616         ;
617
618 enums:                                          { $$ = NULL; }
619         | enum_list ','                         { $$ = $1; }
620         | enum_list
621         ;
622
623 enum_list: enum                                 { if (!$1->eval)
624                                                     $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
625                                                   $$ = append_var( NULL, $1 );
626                                                 }
627         | enum_list ',' enum                    { if (!$3->eval)
628                                                   {
629                                                     var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
630                                                     $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
631                                                   }
632                                                   $$ = append_var( $1, $3 );
633                                                 }
634         ;
635
636 enum:     ident '=' expr_int_const              { $$ = reg_const($1);
637                                                   $$->eval = $3;
638                                                   $$->type = make_int(0);
639                                                 }
640         | ident                                 { $$ = reg_const($1);
641                                                   $$->type = make_int(0);
642                                                 }
643         ;
644
645 enumdef: tENUM t_ident '{' enums '}'            { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
646                                                   $$->kind = TKIND_ENUM;
647                                                   $$->fields_or_args = $4;
648                                                   $$->defined = TRUE;
649                                                   if(in_typelib)
650                                                       add_typelib_entry($$);
651                                                 }
652         ;
653
654 m_exprs:  m_expr                                { $$ = append_expr( NULL, $1 ); }
655         | m_exprs ',' m_expr                    { $$ = append_expr( $1, $3 ); }
656         ;
657
658 /*
659 exprs:                                          { $$ = make_expr(EXPR_VOID); }
660         | expr_list
661         ;
662
663 expr_list: expr
664         | expr_list ',' expr                    { LINK($3, $1); $$ = $3; }
665         ;
666 */
667
668 m_expr:                                         { $$ = make_expr(EXPR_VOID); }
669         | expr
670         ;
671
672 expr:     aNUM                                  { $$ = make_exprl(EXPR_NUM, $1); }
673         | aHEXNUM                               { $$ = make_exprl(EXPR_HEXNUM, $1); }
674         | aDOUBLE                               { $$ = make_exprd(EXPR_DOUBLE, $1); }
675         | tFALSE                                { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
676         | tNULL                                 { $$ = make_exprl(EXPR_NUM, 0); }
677         | tTRUE                                 { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
678         | aSTRING                               { $$ = make_exprs(EXPR_STRLIT, $1); }
679         | aWSTRING                              { $$ = make_exprs(EXPR_WSTRLIT, $1); }
680         | aIDENTIFIER                           { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
681         | expr '?' expr ':' expr                { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
682         | expr LOGICALOR expr                   { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
683         | expr LOGICALAND expr                  { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
684         | expr '|' expr                         { $$ = make_expr2(EXPR_OR , $1, $3); }
685         | expr '^' expr                         { $$ = make_expr2(EXPR_XOR, $1, $3); }
686         | expr '&' expr                         { $$ = make_expr2(EXPR_AND, $1, $3); }
687         | expr EQUALITY expr                    { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
688         | expr INEQUALITY expr                  { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
689         | expr '>' expr                         { $$ = make_expr2(EXPR_GTR, $1, $3); }
690         | expr '<' expr                         { $$ = make_expr2(EXPR_LESS, $1, $3); }
691         | expr GREATEREQUAL expr                { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
692         | expr LESSEQUAL expr                   { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
693         | expr SHL expr                         { $$ = make_expr2(EXPR_SHL, $1, $3); }
694         | expr SHR expr                         { $$ = make_expr2(EXPR_SHR, $1, $3); }
695         | expr '+' expr                         { $$ = make_expr2(EXPR_ADD, $1, $3); }
696         | expr '-' expr                         { $$ = make_expr2(EXPR_SUB, $1, $3); }
697         | expr '%' expr                         { $$ = make_expr2(EXPR_MOD, $1, $3); }
698         | expr '*' expr                         { $$ = make_expr2(EXPR_MUL, $1, $3); }
699         | expr '/' expr                         { $$ = make_expr2(EXPR_DIV, $1, $3); }
700         | '!' expr                              { $$ = make_expr1(EXPR_LOGNOT, $2); }
701         | '~' expr                              { $$ = make_expr1(EXPR_NOT, $2); }
702         | '+' expr %prec POS                    { $$ = make_expr1(EXPR_POS, $2); }
703         | '-' expr %prec NEG                    { $$ = make_expr1(EXPR_NEG, $2); }
704         | '&' expr %prec ADDRESSOF              { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
705         | '*' expr %prec PPTR                   { $$ = make_expr1(EXPR_PPTR, $2); }
706         | expr MEMBERPTR aIDENTIFIER            { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
707         | expr '.' aIDENTIFIER                  { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
708         | '(' type ')' expr %prec CAST          { $$ = make_exprt(EXPR_CAST, $2, $4); }
709         | tSIZEOF '(' type ')'                  { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
710         | expr '[' expr ']'                     { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
711         | '(' expr ')'                          { $$ = $2; }
712         ;
713
714 expr_list_int_const: expr_int_const             { $$ = append_expr( NULL, $1 ); }
715         | expr_list_int_const ',' expr_int_const        { $$ = append_expr( $1, $3 ); }
716         ;
717
718 expr_int_const: expr                            { $$ = $1;
719                                                   if (!$$->is_const)
720                                                       error_loc("expression is not an integer constant\n");
721                                                 }
722         ;
723
724 expr_const: expr                                { $$ = $1;
725                                                   if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
726                                                       error_loc("expression is not constant\n");
727                                                 }
728         ;
729
730 fields:                                         { $$ = NULL; }
731         | fields field                          { $$ = append_var_list($1, $2); }
732         ;
733
734 field:    m_attributes decl_spec declarator_list ';'
735                                                 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
736                                                   check_field_attrs(first, $1);
737                                                   $$ = set_var_types($1, $2, $3);
738                                                 }
739         | m_attributes uniondef ';'             { var_t *v = make_var(NULL);
740                                                   v->type = $2; v->attrs = $1;
741                                                   $$ = append_var(NULL, v);
742                                                 }
743         ;
744
745 ne_union_field:
746           s_field ';'                           { $$ = $1; }
747         | attributes ';'                        { $$ = make_var(NULL); $$->attrs = $1; }
748         ;
749
750 ne_union_fields:                                { $$ = NULL; }
751         | ne_union_fields ne_union_field        { $$ = append_var( $1, $2 ); }
752         ;
753
754 union_field:
755           s_field ';'                           { $$ = $1; }
756         | ';'                                   { $$ = NULL; }
757         ;
758
759 s_field:  m_attributes decl_spec declarator     { $$ = $3->var;
760                                                   $$->attrs = check_field_attrs($$->name, $1);
761                                                   set_type($$, $2, $3, FALSE);
762                                                   free($3);
763                                                 }
764         ;
765
766 funcdef:
767           m_attributes decl_spec declarator     { var_t *v = $3->var;
768                                                   v->attrs = check_function_attrs(v->name, $1);
769                                                   set_type(v, $2, $3, FALSE);
770                                                   free($3);
771                                                   $$ = make_func(v);
772                                                 }
773         ;
774
775 declaration:
776           attributes decl_spec init_declarator
777                                                 { $$ = $3->var;
778                                                   $$->attrs = $1;
779                                                   set_type($$, $2, $3, FALSE);
780                                                   free($3);
781                                                 }
782         | decl_spec init_declarator             { $$ = $2->var;
783                                                   set_type($$, $1, $2, FALSE);
784                                                   free($2);
785                                                 }
786         ;
787
788 m_ident:                                        { $$ = NULL; }
789         | ident
790         ;
791
792 t_ident:                                        { $$ = NULL; }
793         | aIDENTIFIER                           { $$ = make_var($1); }
794         | aKNOWNTYPE                            { $$ = make_var($1); }
795         ;
796
797 ident:    aIDENTIFIER                           { $$ = make_var($1); }
798 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
799         | aKNOWNTYPE                            { $$ = make_var($<str>1); }
800         ;
801
802 base_type: tBYTE                                { $$ = make_builtin($<str>1); }
803         | tWCHAR                                { $$ = make_builtin($<str>1); }
804         | int_std
805         | tSIGNED int_std                       { $$ = $2; $$->sign = 1; }
806         | tUNSIGNED int_std                     { $$ = $2; $$->sign = -1;
807                                                   switch ($$->type) {
808                                                   case RPC_FC_CHAR:  break;
809                                                   case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
810                                                   case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
811                                                   case RPC_FC_LONG:  $$->type = RPC_FC_ULONG;  break;
812                                                   case RPC_FC_HYPER:
813                                                     if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
814                                                     {
815                                                       $$ = alias($$, "MIDL_uhyper");
816                                                       $$->sign = 0;
817                                                     }
818                                                     break;
819                                                   default: break;
820                                                   }
821                                                 }
822         | tUNSIGNED                             { $$ = make_int(-1); }
823         | tFLOAT                                { $$ = make_builtin($<str>1); }
824         | tSINGLE                               { $$ = find_type("float", 0); }
825         | tDOUBLE                               { $$ = make_builtin($<str>1); }
826         | tBOOLEAN                              { $$ = make_builtin($<str>1); }
827         | tERRORSTATUST                         { $$ = make_builtin($<str>1); }
828         | tHANDLET                              { $$ = make_builtin($<str>1); }
829         ;
830
831 m_int:
832         | tINT
833         ;
834
835 int_std:  tINT                                  { $$ = make_builtin($<str>1); }
836         | tSHORT m_int                          { $$ = make_builtin($<str>1); }
837         | tSMALL                                { $$ = make_builtin($<str>1); }
838         | tLONG m_int                           { $$ = make_builtin($<str>1); }
839         | tHYPER m_int                          { $$ = make_builtin($<str>1); }
840         | tINT64                                { $$ = make_builtin($<str>1); }
841         | tCHAR                                 { $$ = make_builtin($<str>1); }
842         ;
843
844 coclass:  tCOCLASS aIDENTIFIER                  { $$ = make_class($2); }
845         | tCOCLASS aKNOWNTYPE                   { $$ = find_type($2, 0);
846                                                   if ($$->kind != TKIND_COCLASS)
847                                                     error_loc("%s was not declared a coclass at %s:%d\n",
848                                                               $2, $$->loc_info.input_name,
849                                                               $$->loc_info.line_number);
850                                                 }
851         ;
852
853 coclasshdr: attributes coclass                  { $$ = $2;
854                                                   check_def($$);
855                                                   $$->attrs = check_coclass_attrs($2->name, $1);
856                                                   if (!parse_only && do_header)
857                                                     write_coclass($$);
858                                                   if (!parse_only && do_idfile)
859                                                     write_clsid($$);
860                                                 }
861         ;
862
863 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
864                                                 { $$ = $1;
865                                                   $$->ifaces = $3;
866                                                   $$->defined = TRUE;
867                                                 }
868         ;
869
870 coclass_ints:                                   { $$ = NULL; }
871         | coclass_ints coclass_int              { $$ = append_ifref( $1, $2 ); }
872         ;
873
874 coclass_int:
875           m_attributes interfacedec             { $$ = make_ifref($2); $$->attrs = $1; }
876         ;
877
878 dispinterface: tDISPINTERFACE aIDENTIFIER       { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
879         |      tDISPINTERFACE aKNOWNTYPE        { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
880         ;
881
882 dispinterfacehdr: attributes dispinterface      { attr_t *attrs;
883                                                   is_in_interface = TRUE;
884                                                   is_object_interface = TRUE;
885                                                   $$ = $2;
886                                                   check_def($$);
887                                                   attrs = make_attr(ATTR_DISPINTERFACE);
888                                                   $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
889                                                   $$->ref = find_type("IDispatch", 0);
890                                                   if (!$$->ref) error_loc("IDispatch is undefined\n");
891                                                   $$->defined = TRUE;
892                                                   if (!parse_only && do_header) write_forward($$);
893                                                 }
894         ;
895
896 dispint_props: tPROPERTIES ':'                  { $$ = NULL; }
897         | dispint_props s_field ';'             { $$ = append_var( $1, $2 ); }
898         ;
899
900 dispint_meths: tMETHODS ':'                     { $$ = NULL; }
901         | dispint_meths funcdef ';'             { $$ = append_func( $1, $2 ); }
902         ;
903
904 dispinterfacedef: dispinterfacehdr '{'
905           dispint_props
906           dispint_meths
907           '}'                                   { $$ = $1;
908                                                   $$->fields_or_args = $3;
909                                                   $$->funcs = $4;
910                                                   if (!parse_only && do_header) write_interface($$);
911                                                   if (!parse_only && do_idfile) write_diid($$);
912                                                   is_in_interface = FALSE;
913                                                 }
914         | dispinterfacehdr
915          '{' interface ';' '}'                  { $$ = $1;
916                                                   $$->fields_or_args = $3->fields_or_args;
917                                                   $$->funcs = $3->funcs;
918                                                   if (!parse_only && do_header) write_interface($$);
919                                                   if (!parse_only && do_idfile) write_diid($$);
920                                                   is_in_interface = FALSE;
921                                                 }
922         ;
923
924 inherit:                                        { $$ = NULL; }
925         | ':' aKNOWNTYPE                        { $$ = find_type_or_error2($2, 0); }
926         ;
927
928 interface: tINTERFACE aIDENTIFIER               { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
929         |  tINTERFACE aKNOWNTYPE                { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
930         ;
931
932 interfacehdr: attributes interface              { $$.interface = $2;
933                                                   $$.old_pointer_default = pointer_default;
934                                                   if (is_attr($1, ATTR_POINTERDEFAULT))
935                                                     pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
936                                                   is_object_interface = is_object($1);
937                                                   is_in_interface = TRUE;
938                                                   check_def($2);
939                                                   $2->attrs = check_iface_attrs($2->name, $1);
940                                                   $2->defined = TRUE;
941                                                   if (!parse_only && do_header) write_forward($2);
942                                                 }
943         ;
944
945 interfacedef: interfacehdr inherit
946           '{' int_statements '}' semicolon_opt  { $$ = $1.interface;
947                                                   $$->ref = $2;
948                                                   $$->funcs = $4;
949                                                   check_functions($$);
950                                                   compute_method_indexes($$);
951                                                   if (!parse_only && do_header) write_interface($$);
952                                                   if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
953                                                   if (!parse_only && do_idfile) write_iid($$);
954                                                   pointer_default = $1.old_pointer_default;
955                                                   is_in_interface = FALSE;
956                                                 }
957 /* MIDL is able to import the definition of a base class from inside the
958  * definition of a derived class, I'll try to support it with this rule */
959         | interfacehdr ':' aIDENTIFIER
960           '{' import int_statements '}'
961            semicolon_opt                        { $$ = $1.interface;
962                                                   $$->ref = find_type_or_error2($3, 0);
963                                                   if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
964                                                   $$->funcs = $6;
965                                                   compute_method_indexes($$);
966                                                   if (!parse_only && do_header) write_interface($$);
967                                                   if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
968                                                   if (!parse_only && do_idfile) write_iid($$);
969                                                   pointer_default = $1.old_pointer_default;
970                                                   is_in_interface = FALSE;
971                                                 }
972         | dispinterfacedef semicolon_opt        { $$ = $1; }
973         ;
974
975 interfacedec:
976           interface ';'                         { $$ = $1; if (!parse_only && do_header) write_forward($$); }
977         | dispinterface ';'                     { $$ = $1; if (!parse_only && do_header) write_forward($$); }
978         ;
979
980 module:   tMODULE aIDENTIFIER                   { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
981         | tMODULE aKNOWNTYPE                    { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
982         ;
983
984 modulehdr: attributes module                    { $$ = $2;
985                                                   $$->attrs = check_module_attrs($2->name, $1);
986                                                 }
987         ;
988
989 moduledef: modulehdr '{' int_statements '}'
990            semicolon_opt                        { $$ = $1;
991                                                   $$->funcs = $3;
992                                                   /* FIXME: if (!parse_only && do_header) write_module($$); */
993                                                 }
994         ;
995
996 storage_cls_spec:
997           tEXTERN                               { $$ = STG_EXTERN; }
998         | tSTATIC                               { $$ = STG_STATIC; }
999         | tREGISTER                             { $$ = STG_REGISTER; }
1000         ;
1001
1002 function_specifier:
1003           tINLINE                               { $$ = make_attr(ATTR_INLINE); }
1004         ;
1005
1006 type_qualifier:
1007           tCONST                                { $$ = make_attr(ATTR_CONST); }
1008         ;
1009
1010 m_type_qual_list:                               { $$ = NULL; }
1011         | m_type_qual_list type_qualifier       { $$ = append_attr($1, $2); }
1012         ;
1013
1014 decl_spec: type m_decl_spec_no_type             { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
1015         | decl_spec_no_type type m_decl_spec_no_type
1016                                                 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
1017         ;
1018
1019 m_decl_spec_no_type:                            { $$ = NULL; }
1020         | decl_spec_no_type
1021         ;
1022
1023 decl_spec_no_type:
1024           type_qualifier m_decl_spec_no_type    { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
1025         | function_specifier m_decl_spec_no_type  { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
1026         | storage_cls_spec m_decl_spec_no_type  { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
1027         ;
1028
1029 declarator:
1030           '*' m_type_qual_list declarator %prec PPTR
1031                                                 { $$ = $3; $$->type = append_ptrchain_type($$->type, make_pointer_type(NULL, $2)); }
1032         | callconv declarator                   { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1033         | direct_declarator
1034         ;
1035
1036 direct_declarator:
1037           ident                                 { $$ = make_declarator($1); }
1038         | '(' declarator ')'                    { $$ = $2; }
1039         | direct_declarator array               { $$ = $1; $$->array = append_array($$->array, $2); }
1040         | direct_declarator '(' m_args ')'      { $$ = $1;
1041                                                   $$->func_type = append_ptrchain_type($$->type, make_func_type($3));
1042                                                   $$->type = NULL;
1043                                                 }
1044         ;
1045
1046 declarator_list:
1047           declarator                            { $$ = append_declarator( NULL, $1 ); }
1048         | declarator_list ',' declarator        { $$ = append_declarator( $1, $3 ); }
1049         ;
1050
1051 init_declarator:
1052           declarator                            { $$ = $1; }
1053         | declarator '=' expr_const             { $$ = $1; $1->var->eval = $3; }
1054         ;
1055
1056 pointer_type:
1057           tREF                                  { $$ = RPC_FC_RP; }
1058         | tUNIQUE                               { $$ = RPC_FC_UP; }
1059         | tPTR                                  { $$ = RPC_FC_FP; }
1060         ;
1061
1062 structdef: tSTRUCT t_ident '{' fields '}'       { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
1063                                                   check_def($$);
1064                                                   $$->kind = TKIND_RECORD;
1065                                                   $$->fields_or_args = $4;
1066                                                   $$->defined = TRUE;
1067                                                   if(in_typelib)
1068                                                       add_typelib_entry($$);
1069                                                 }
1070         ;
1071
1072 type:     tVOID                                 { $$ = find_type_or_error("void", 0); }
1073         | aKNOWNTYPE                            { $$ = find_type_or_error($1, 0); }
1074         | base_type                             { $$ = $1; }
1075         | enumdef                               { $$ = $1; }
1076         | tENUM aIDENTIFIER                     { $$ = find_type_or_error2($2, tsENUM); }
1077         | structdef                             { $$ = $1; }
1078         | tSTRUCT aIDENTIFIER                   { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
1079         | uniondef                              { $$ = $1; }
1080         | tUNION aIDENTIFIER                    { $$ = find_type_or_error2($2, tsUNION); }
1081         | tSAFEARRAY '(' type ')'               { $$ = make_safearray($3); }
1082         ;
1083
1084 typedef: tTYPEDEF m_attributes decl_spec declarator_list
1085                                                 { reg_typedefs($3, $4, check_typedef_attrs($2));
1086                                                   $$ = process_typedefs($4);
1087                                                 }
1088         ;
1089
1090 uniondef: tUNION t_ident '{' ne_union_fields '}'
1091                                                 { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
1092                                                   check_def($$);
1093                                                   $$->kind = TKIND_UNION;
1094                                                   $$->fields_or_args = $4;
1095                                                   $$->defined = TRUE;
1096                                                 }
1097         | tUNION t_ident
1098           tSWITCH '(' s_field ')'
1099           m_ident '{' cases '}'                 { var_t *u = $7;
1100                                                   $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
1101                                                   check_def($$);
1102                                                   $$->kind = TKIND_UNION;
1103                                                   if (!u) u = make_var( xstrdup("tagged_union") );
1104                                                   u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
1105                                                   u->type->kind = TKIND_UNION;
1106                                                   u->type->fields_or_args = $9;
1107                                                   u->type->defined = TRUE;
1108                                                   $$->fields_or_args = append_var( $$->fields_or_args, $5 );
1109                                                   $$->fields_or_args = append_var( $$->fields_or_args, u );
1110                                                   $$->defined = TRUE;
1111                                                 }
1112         ;
1113
1114 version:
1115           aNUM                                  { $$ = MAKEVERSION($1, 0); }
1116         | aNUM '.' aNUM                         { $$ = MAKEVERSION($1, $3); }
1117         ;
1118
1119 %%
1120
1121 static void decl_builtin(const char *name, unsigned char type)
1122 {
1123   type_t *t = make_type(type, NULL);
1124   t->name = xstrdup(name);
1125   reg_type(t, name, 0);
1126 }
1127
1128 static type_t *make_builtin(char *name)
1129 {
1130   /* NAME is strdup'd in the lexer */
1131   type_t *t = duptype(find_type_or_error(name, 0), 0);
1132   t->name = name;
1133   return t;
1134 }
1135
1136 static type_t *make_int(int sign)
1137 {
1138   type_t *t = duptype(find_type_or_error("int", 0), 1);
1139
1140   t->sign = sign;
1141   if (sign < 0)
1142     t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
1143
1144   return t;
1145 }
1146
1147 void init_types(void)
1148 {
1149   decl_builtin("void", 0);
1150   decl_builtin("byte", RPC_FC_BYTE);
1151   decl_builtin("wchar_t", RPC_FC_WCHAR);
1152   decl_builtin("int", RPC_FC_LONG);     /* win32 */
1153   decl_builtin("short", RPC_FC_SHORT);
1154   decl_builtin("small", RPC_FC_SMALL);
1155   decl_builtin("long", RPC_FC_LONG);
1156   decl_builtin("hyper", RPC_FC_HYPER);
1157   decl_builtin("__int64", RPC_FC_HYPER);
1158   decl_builtin("char", RPC_FC_CHAR);
1159   decl_builtin("float", RPC_FC_FLOAT);
1160   decl_builtin("double", RPC_FC_DOUBLE);
1161   decl_builtin("boolean", RPC_FC_BYTE);
1162   decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1163   decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1164 }
1165
1166 static str_list_t *append_str(str_list_t *list, char *str)
1167 {
1168     struct str_list_entry_t *entry;
1169
1170     if (!str) return list;
1171     if (!list)
1172     {
1173         list = xmalloc( sizeof(*list) );
1174         list_init( list );
1175     }
1176     entry = xmalloc( sizeof(*entry) );
1177     entry->str = str;
1178     list_add_tail( list, &entry->entry );
1179     return list;
1180 }
1181
1182 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1183 {
1184     attr_t *attr_existing;
1185     if (!attr) return list;
1186     if (!list)
1187     {
1188         list = xmalloc( sizeof(*list) );
1189         list_init( list );
1190     }
1191     LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1192         if (attr_existing->type == attr->type)
1193         {
1194             parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1195             /* use the last attribute, like MIDL does */
1196             list_remove(&attr_existing->entry);
1197             break;
1198         }
1199     list_add_tail( list, &attr->entry );
1200     return list;
1201 }
1202
1203 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1204 {
1205   attr_t *attr;
1206   if (!src) return dst;
1207   LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1208     if (attr->type == type)
1209     {
1210       list_remove(&attr->entry);
1211       return append_attr(dst, attr);
1212     }
1213   return dst;
1214 }
1215
1216 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1217 {
1218   struct list *entry;
1219
1220   if (!old_list) return new_list;
1221
1222   while ((entry = list_head(old_list)))
1223   {
1224     attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1225     list_remove(entry);
1226     new_list = append_attr(new_list, attr);
1227   }
1228   return new_list;
1229 }
1230
1231 static attr_list_t *dupattrs(const attr_list_t *list)
1232 {
1233   attr_list_t *new_list;
1234   const attr_t *attr;
1235
1236   if (!list) return NULL;
1237
1238   new_list = xmalloc( sizeof(*list) );
1239   list_init( new_list );
1240   LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1241   {
1242     attr_t *new_attr = xmalloc(sizeof(*new_attr));
1243     *new_attr = *attr;
1244     list_add_tail(new_list, &new_attr->entry);
1245   }
1246   return new_list;
1247 }
1248
1249 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass)
1250 {
1251   decl_spec_t *declspec = left ? left : right;
1252   if (!declspec)
1253   {
1254     declspec = xmalloc(sizeof(*declspec));
1255     declspec->type = NULL;
1256     declspec->attrs = NULL;
1257     declspec->stgclass = STG_NONE;
1258   }
1259   declspec->type = type;
1260   if (left && declspec != left)
1261   {
1262     declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1263     if (declspec->stgclass == STG_NONE)
1264       declspec->stgclass = left->stgclass;
1265     else if (left->stgclass != STG_NONE)
1266       error_loc("only one storage class can be specified\n");
1267     assert(!left->type);
1268     free(left);
1269   }
1270   if (right && declspec != right)
1271   {
1272     declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1273     if (declspec->stgclass == STG_NONE)
1274       declspec->stgclass = right->stgclass;
1275     else if (right->stgclass != STG_NONE)
1276       error_loc("only one storage class can be specified\n");
1277     assert(!right->type);
1278     free(right);
1279   }
1280
1281   declspec->attrs = append_attr(declspec->attrs, attr);
1282   if (declspec->stgclass == STG_NONE)
1283     declspec->stgclass = stgclass;
1284   else if (stgclass != STG_NONE)
1285     error_loc("only one storage class can be specified\n");
1286
1287   /* apply attributes to type */
1288   if (type && declspec->attrs)
1289   {
1290     attr_list_t *attrs;
1291     declspec->type = duptype(type, 1);
1292     attrs = dupattrs(type->attrs);
1293     declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1294     declspec->attrs = NULL;
1295   }
1296
1297   return declspec;
1298 }
1299
1300 static attr_t *make_attr(enum attr_type type)
1301 {
1302   attr_t *a = xmalloc(sizeof(attr_t));
1303   a->type = type;
1304   a->u.ival = 0;
1305   return a;
1306 }
1307
1308 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1309 {
1310   attr_t *a = xmalloc(sizeof(attr_t));
1311   a->type = type;
1312   a->u.ival = val;
1313   return a;
1314 }
1315
1316 static attr_t *make_attrp(enum attr_type type, void *val)
1317 {
1318   attr_t *a = xmalloc(sizeof(attr_t));
1319   a->type = type;
1320   a->u.pval = val;
1321   return a;
1322 }
1323
1324 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1325 {
1326     if (!expr) return list;
1327     if (!list)
1328     {
1329         list = xmalloc( sizeof(*list) );
1330         list_init( list );
1331     }
1332     list_add_tail( list, &expr->entry );
1333     return list;
1334 }
1335
1336 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1337 {
1338     if (!expr) return list;
1339     if (!list)
1340     {
1341         list = xmalloc( sizeof(*list) );
1342         list_init( list );
1343     }
1344     list_add_tail( list, &expr->entry );
1345     return list;
1346 }
1347
1348 static struct list type_pool = LIST_INIT(type_pool);
1349 typedef struct
1350 {
1351   type_t data;
1352   struct list link;
1353 } type_pool_node_t;
1354
1355 type_t *alloc_type(void)
1356 {
1357   type_pool_node_t *node = xmalloc(sizeof *node);
1358   list_add_tail(&type_pool, &node->link);
1359   return &node->data;
1360 }
1361
1362 void set_all_tfswrite(int val)
1363 {
1364   type_pool_node_t *node;
1365   LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1366     node->data.tfswrite = val;
1367 }
1368
1369 void clear_all_offsets(void)
1370 {
1371   type_pool_node_t *node;
1372   LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1373     node->data.typestring_offset = node->data.ptrdesc = 0;
1374 }
1375
1376 type_t *make_type(unsigned char type, type_t *ref)
1377 {
1378   type_t *t = alloc_type();
1379   t->name = NULL;
1380   t->kind = TKIND_PRIMITIVE;
1381   t->type = type;
1382   t->ref = ref;
1383   t->attrs = NULL;
1384   t->orig = NULL;
1385   t->funcs = NULL;
1386   t->fields_or_args = NULL;
1387   t->ifaces = NULL;
1388   t->dim = 0;
1389   t->size_is = NULL;
1390   t->length_is = NULL;
1391   t->typestring_offset = 0;
1392   t->ptrdesc = 0;
1393   t->declarray = FALSE;
1394   t->ignore = (parse_only != 0);
1395   t->sign = 0;
1396   t->defined = FALSE;
1397   t->written = FALSE;
1398   t->user_types_registered = FALSE;
1399   t->tfswrite = FALSE;
1400   t->checked = FALSE;
1401   t->typelib_idx = -1;
1402   init_loc_info(&t->loc_info);
1403   return t;
1404 }
1405
1406 static type_t *make_func_type(var_list_t *args)
1407 {
1408   type_t *t = make_type(RPC_FC_FUNCTION, NULL);
1409   t->fields_or_args = args;
1410   return t;
1411 }
1412
1413 static type_t *make_pointer_type(type_t *ref, attr_list_t *attrs)
1414 {
1415     type_t *t = make_type(pointer_default, ref);
1416     t->attrs = attrs;
1417     return t;
1418 }
1419
1420 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1421 {
1422   type_t *ptrchain_type;
1423   if (!ptrchain)
1424     return type;
1425   for (ptrchain_type = ptrchain; ptrchain_type->ref; ptrchain_type = ptrchain_type->ref)
1426     ;
1427   ptrchain_type->ref = type;
1428   return ptrchain;
1429 }
1430
1431 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl,
1432                      int top)
1433 {
1434   expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1435   expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1436   int sizeless, has_varconf;
1437   expr_t *dim;
1438   type_t *atype, **ptype;
1439   array_dims_t *arr = decl ? decl->array : NULL;
1440   type_t *func_type = decl ? decl->func_type : NULL;
1441   type_t *type = decl_spec->type;
1442
1443   if (is_attr(type->attrs, ATTR_INLINE))
1444   {
1445     if (!func_type)
1446       error_loc("inline attribute applied to non-function type\n");
1447     else
1448     {
1449       type_t *t;
1450       /* move inline attribute from return type node to function node */
1451       for (t = func_type; is_ptr(t); t = t->ref)
1452         ;
1453       t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1454     }
1455   }
1456
1457   /* add type onto the end of the pointers in pident->type */
1458   v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1459   v->stgclass = decl_spec->stgclass;
1460
1461   /* the highest level of pointer specified should default to the var's ptr attr
1462    * or (RPC_FC_RP if not specified and it's a top level ptr), not
1463    * pointer_default so we need to fix that up here */
1464   if (!arr)
1465   {
1466     int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1467     const type_t *ptr = NULL;
1468     /* pointer attributes on the left side of the type belong to the function
1469      * pointer, if one is being declared */
1470     type_t **pt = func_type ? &func_type : &v->type;
1471     for (ptr = *pt; ptr && !ptr_attr; )
1472     {
1473       ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1474       if (!ptr_attr && ptr->kind == TKIND_ALIAS)
1475         ptr = ptr->orig;
1476       else
1477         break;
1478     }
1479     if (ptr && is_ptr(ptr) && (ptr_attr || top))
1480     {
1481       /* duplicate type to avoid changing original type */
1482       *pt = duptype(*pt, 1);
1483       (*pt)->type = ptr_attr ? ptr_attr : RPC_FC_RP;
1484     }
1485     else if (ptr_attr)
1486        error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1487   }
1488
1489   if (is_attr(v->attrs, ATTR_STRING) && !is_ptr(v->type) && !arr)
1490     error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1491               v->name);
1492
1493   if (is_attr(v->attrs, ATTR_V1ENUM))
1494   {
1495     if (v->type->type == RPC_FC_ENUM16)
1496       v->type->type = RPC_FC_ENUM32;
1497     else
1498       error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1499   }
1500
1501   sizeless = FALSE;
1502   if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1503   {
1504     if (sizeless)
1505       error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1506
1507     if (dim->is_const)
1508     {
1509       v->type = make_type(RPC_FC_LGFARRAY, v->type);
1510     }
1511     else
1512     {
1513       sizeless = TRUE;
1514       v->type = make_type(RPC_FC_CARRAY, v->type);
1515     }
1516
1517     v->type->declarray = TRUE;
1518     v->type->dim = dim->cval;
1519   }
1520
1521   ptype = &v->type;
1522   has_varconf = FALSE;
1523   if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1524   {
1525     if (dim->type != EXPR_VOID)
1526     {
1527       has_varconf = TRUE;
1528       atype = *ptype = duptype(*ptype, 0);
1529
1530       if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1531         error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1532
1533       if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1534         error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1535
1536       atype->type = RPC_FC_CARRAY;
1537       atype->size_is = dim;
1538     }
1539
1540     ptype = &(*ptype)->ref;
1541     if (*ptype == NULL)
1542       error_loc("%s: too many expressions in size_is attribute\n", v->name);
1543   }
1544
1545   ptype = &v->type;
1546   if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1547   {
1548     if (dim->type != EXPR_VOID)
1549     {
1550       has_varconf = TRUE;
1551       atype = *ptype = duptype(*ptype, 0);
1552
1553       if (atype->type == RPC_FC_SMFARRAY)
1554         atype->type = RPC_FC_SMVARRAY;
1555       else if (atype->type == RPC_FC_LGFARRAY)
1556         atype->type = RPC_FC_LGVARRAY;
1557       else if (atype->type == RPC_FC_CARRAY)
1558         atype->type = RPC_FC_CVARRAY;
1559       else
1560         error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1561
1562       atype->length_is = dim;
1563     }
1564
1565     ptype = &(*ptype)->ref;
1566     if (*ptype == NULL)
1567       error_loc("%s: too many expressions in length_is attribute\n", v->name);
1568   }
1569
1570   if (has_varconf && !last_array(v->type))
1571   {
1572     ptype = &v->type;
1573     for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1574     {
1575       *ptype = duptype(*ptype, 0);
1576       (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1577     }
1578   }
1579
1580   /* v->type is currently pointing to the type on the left-side of the
1581    * declaration, so we need to fix this up so that it is the return type of the
1582    * function and make v->type point to the function side of the declaration */
1583   if (func_type)
1584   {
1585     type_t *ft, *t;
1586     type_t *return_type = v->type;
1587     v->type = func_type;
1588     for (ft = v->type; is_ptr(ft); ft = ft->ref)
1589       ;
1590     assert(ft->type == RPC_FC_FUNCTION);
1591     ft->ref = return_type;
1592     /* move calling convention attribute, if present, from pointer nodes to
1593      * function node */
1594     for (t = v->type; is_ptr(t); t = t->ref)
1595       ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1596     if (is_object_interface && !is_attr(ft->attrs, ATTR_CALLCONV))
1597     {
1598       static char *stdmethodcalltype;
1599       if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1600       ft->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1601     }
1602   }
1603   else
1604   {
1605     type_t *t;
1606     for (t = v->type; is_ptr(t); t = t->ref)
1607       if (is_attr(t->attrs, ATTR_CALLCONV))
1608         error_loc("calling convention applied to non-function-pointer type\n");
1609   }
1610 }
1611
1612 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1613 {
1614   declarator_t *decl, *next;
1615   var_list_t *var_list = NULL;
1616
1617   LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1618   {
1619     var_t *var = decl->var;
1620
1621     var->attrs = attrs;
1622     set_type(var, decl_spec, decl, 0);
1623     var_list = append_var(var_list, var);
1624     free(decl);
1625   }
1626   return var_list;
1627 }
1628
1629 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1630 {
1631     if (!iface) return list;
1632     if (!list)
1633     {
1634         list = xmalloc( sizeof(*list) );
1635         list_init( list );
1636     }
1637     list_add_tail( list, &iface->entry );
1638     return list;
1639 }
1640
1641 static ifref_t *make_ifref(type_t *iface)
1642 {
1643   ifref_t *l = xmalloc(sizeof(ifref_t));
1644   l->iface = iface;
1645   l->attrs = NULL;
1646   return l;
1647 }
1648
1649 static var_list_t *append_var(var_list_t *list, var_t *var)
1650 {
1651     if (!var) return list;
1652     if (!list)
1653     {
1654         list = xmalloc( sizeof(*list) );
1655         list_init( list );
1656     }
1657     list_add_tail( list, &var->entry );
1658     return list;
1659 }
1660
1661 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1662 {
1663     if (!vars) return list;
1664     if (!list)
1665     {
1666         list = xmalloc( sizeof(*list) );
1667         list_init( list );
1668     }
1669     list_move_tail( list, vars );
1670     return list;
1671 }
1672
1673 static var_t *make_var(char *name)
1674 {
1675   var_t *v = xmalloc(sizeof(var_t));
1676   v->name = name;
1677   v->type = NULL;
1678   v->attrs = NULL;
1679   v->eval = NULL;
1680   v->stgclass = STG_NONE;
1681   init_loc_info(&v->loc_info);
1682   return v;
1683 }
1684
1685 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1686 {
1687   if (!d) return list;
1688   if (!list) {
1689     list = xmalloc(sizeof(*list));
1690     list_init(list);
1691   }
1692   list_add_tail(list, &d->entry);
1693   return list;
1694 }
1695
1696 static declarator_t *make_declarator(var_t *var)
1697 {
1698   declarator_t *d = xmalloc(sizeof(*d));
1699   d->var = var;
1700   d->type = NULL;
1701   d->func_type = NULL;
1702   d->array = NULL;
1703   return d;
1704 }
1705
1706 static func_list_t *append_func(func_list_t *list, func_t *func)
1707 {
1708     if (!func) return list;
1709     if (!list)
1710     {
1711         list = xmalloc( sizeof(*list) );
1712         list_init( list );
1713     }
1714     list_add_tail( list, &func->entry );
1715     return list;
1716 }
1717
1718 static func_t *make_func(var_t *def)
1719 {
1720   func_t *f = xmalloc(sizeof(func_t));
1721   f->def = def;
1722   f->args = def->type->fields_or_args;
1723   f->ignore = parse_only;
1724   f->idx = -1;
1725   return f;
1726 }
1727
1728 static type_t *make_class(char *name)
1729 {
1730   type_t *c = make_type(RPC_FC_COCLASS, NULL);
1731   c->name = name;
1732   c->kind = TKIND_COCLASS;
1733   return c;
1734 }
1735
1736 static type_t *make_safearray(type_t *type)
1737 {
1738   type_t *sa = find_type_or_error("SAFEARRAY", 0);
1739   sa->ref = type;
1740   return make_type(pointer_default, sa);
1741 }
1742
1743 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1744 {
1745     typelib_t *typelib = xmalloc(sizeof(*typelib));
1746     typelib->name = xstrdup(name);
1747     typelib->filename = NULL;
1748     typelib->attrs = attrs;
1749     list_init( &typelib->entries );
1750     list_init( &typelib->importlibs );
1751     return typelib;
1752 }
1753
1754 #define HASHMAX 64
1755
1756 static int hash_ident(const char *name)
1757 {
1758   const char *p = name;
1759   int sum = 0;
1760   /* a simple sum hash is probably good enough */
1761   while (*p) {
1762     sum += *p;
1763     p++;
1764   }
1765   return sum & (HASHMAX-1);
1766 }
1767
1768 /***** type repository *****/
1769
1770 struct rtype {
1771   const char *name;
1772   type_t *type;
1773   int t;
1774   struct rtype *next;
1775 };
1776
1777 struct rtype *type_hash[HASHMAX];
1778
1779 static type_t *reg_type(type_t *type, const char *name, int t)
1780 {
1781   struct rtype *nt;
1782   int hash;
1783   if (!name) {
1784     error_loc("registering named type without name\n");
1785     return type;
1786   }
1787   hash = hash_ident(name);
1788   nt = xmalloc(sizeof(struct rtype));
1789   nt->name = name;
1790   nt->type = type;
1791   nt->t = t;
1792   nt->next = type_hash[hash];
1793   type_hash[hash] = nt;
1794   return type;
1795 }
1796
1797 static int is_incomplete(const type_t *t)
1798 {
1799   return !t->defined && (is_struct(t->type) || is_union(t->type));
1800 }
1801
1802 static void add_incomplete(type_t *t)
1803 {
1804   struct typenode *tn = xmalloc(sizeof *tn);
1805   tn->type = t;
1806   list_add_tail(&incomplete_types, &tn->entry);
1807 }
1808
1809 static void fix_type(type_t *t)
1810 {
1811   if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1812     type_t *ot = t->orig;
1813     fix_type(ot);
1814     t->fields_or_args = ot->fields_or_args;
1815     t->defined = ot->defined;
1816   }
1817 }
1818
1819 static void fix_incomplete(void)
1820 {
1821   struct typenode *tn, *next;
1822
1823   LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1824     fix_type(tn->type);
1825     free(tn);
1826   }
1827 }
1828
1829 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1830 {
1831   const declarator_t *decl;
1832   int is_str = is_attr(attrs, ATTR_STRING);
1833   type_t *type = decl_spec->type;
1834
1835   if (is_str)
1836   {
1837     type_t *t = decl_spec->type;
1838     unsigned char c;
1839
1840     while (is_ptr(t))
1841       t = t->ref;
1842
1843     c = t->type;
1844     if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1845     {
1846       decl = LIST_ENTRY( list_head( decls ), const declarator_t, entry );
1847       error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1848               decl->var->name);
1849     }
1850   }
1851
1852   /* We must generate names for tagless enum, struct or union.
1853      Typedef-ing a tagless enum, struct or union means we want the typedef
1854      to be included in a library hence the public attribute.  */
1855   if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1856        || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1857   {
1858     if (! is_attr(attrs, ATTR_PUBLIC))
1859       attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1860     type->name = gen_name();
1861   }
1862   else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1863     attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1864
1865   LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1866   {
1867     var_t *name = decl->var;
1868
1869     if (name->name) {
1870       type_t *cur;
1871
1872       cur = find_type(name->name, 0);
1873       if (cur)
1874           error_loc("%s: redefinition error; original definition was at %s:%d\n",
1875                     cur->name, cur->loc_info.input_name,
1876                     cur->loc_info.line_number);
1877
1878       /* set the attributes to allow set_type to do some checks on them */
1879       name->attrs = attrs;
1880       set_type(name, decl_spec, decl, 0);
1881       cur = alias(name->type, name->name);
1882       cur->attrs = attrs;
1883
1884       if (is_incomplete(cur))
1885         add_incomplete(cur);
1886       reg_type(cur, cur->name, 0);
1887     }
1888   }
1889   return type;
1890 }
1891
1892 type_t *find_type(const char *name, int t)
1893 {
1894   struct rtype *cur = type_hash[hash_ident(name)];
1895   while (cur && (cur->t != t || strcmp(cur->name, name)))
1896     cur = cur->next;
1897   return cur ? cur->type : NULL;
1898 }
1899
1900 static type_t *find_type_or_error(const char *name, int t)
1901 {
1902   type_t *type = find_type(name, t);
1903   if (!type) {
1904     error_loc("type '%s' not found\n", name);
1905     return NULL;
1906   }
1907   return type;
1908 }
1909
1910 static type_t *find_type_or_error2(char *name, int t)
1911 {
1912   type_t *tp = find_type_or_error(name, t);
1913   free(name);
1914   return tp;
1915 }
1916
1917 int is_type(const char *name)
1918 {
1919   return find_type(name, 0) != NULL;
1920 }
1921
1922 static type_t *get_type(unsigned char type, char *name, int t)
1923 {
1924   type_t *tp;
1925   if (name) {
1926     tp = find_type(name, t);
1927     if (tp) {
1928       free(name);
1929       return tp;
1930     }
1931   }
1932   tp = make_type(type, NULL);
1933   tp->name = name;
1934   if (!name) return tp;
1935   return reg_type(tp, name, t);
1936 }
1937
1938 static type_t *get_typev(unsigned char type, var_t *name, int t)
1939 {
1940   char *sname = NULL;
1941   if (name) {
1942     sname = name->name;
1943     free(name);
1944   }
1945   return get_type(type, sname, t);
1946 }
1947
1948 /***** constant repository *****/
1949
1950 struct rconst {
1951   char *name;
1952   var_t *var;
1953   struct rconst *next;
1954 };
1955
1956 struct rconst *const_hash[HASHMAX];
1957
1958 static var_t *reg_const(var_t *var)
1959 {
1960   struct rconst *nc;
1961   int hash;
1962   if (!var->name) {
1963     error_loc("registering constant without name\n");
1964     return var;
1965   }
1966   hash = hash_ident(var->name);
1967   nc = xmalloc(sizeof(struct rconst));
1968   nc->name = var->name;
1969   nc->var = var;
1970   nc->next = const_hash[hash];
1971   const_hash[hash] = nc;
1972   return var;
1973 }
1974
1975 var_t *find_const(const char *name, int f)
1976 {
1977   struct rconst *cur = const_hash[hash_ident(name)];
1978   while (cur && strcmp(cur->name, name))
1979     cur = cur->next;
1980   if (!cur) {
1981     if (f) error_loc("constant '%s' not found\n", name);
1982     return NULL;
1983   }
1984   return cur->var;
1985 }
1986
1987 static void write_libid(const typelib_t *typelib)
1988 {
1989   const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
1990   write_guid(idfile, "LIBID", typelib->name, uuid);
1991 }
1992
1993 static void write_clsid(type_t *cls)
1994 {
1995   const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1996   write_guid(idfile, "CLSID", cls->name, uuid);
1997 }
1998
1999 static void write_diid(type_t *iface)
2000 {
2001   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2002   write_guid(idfile, "DIID", iface->name, uuid);
2003 }
2004
2005 static void write_iid(type_t *iface)
2006 {
2007   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2008   write_guid(idfile, "IID", iface->name, uuid);
2009 }
2010
2011 static int compute_method_indexes(type_t *iface)
2012 {
2013   int idx;
2014   func_t *f;
2015
2016   if (iface->ref)
2017     idx = compute_method_indexes(iface->ref);
2018   else
2019     idx = 0;
2020
2021   if (!iface->funcs)
2022     return idx;
2023
2024   LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2025     if (! is_callas(f->def->attrs))
2026       f->idx = idx++;
2027
2028   return idx;
2029 }
2030
2031 static char *gen_name(void)
2032 {
2033   static const char format[] = "__WIDL_%s_generated_name_%08lX";
2034   static unsigned long n = 0;
2035   static const char *file_id;
2036   static size_t size;
2037   char *name;
2038
2039   if (! file_id)
2040   {
2041     char *dst = dup_basename(input_name, ".idl");
2042     file_id = dst;
2043
2044     for (; *dst; ++dst)
2045       if (! isalnum((unsigned char) *dst))
2046         *dst = '_';
2047
2048     size = sizeof format - 7 + strlen(file_id) + 8;
2049   }
2050
2051   name = xmalloc(size);
2052   sprintf(name, format, file_id, n++);
2053   return name;
2054 }
2055
2056 struct allowed_attr
2057 {
2058     unsigned int dce_compatible : 1;
2059     unsigned int acf : 1;
2060     unsigned int on_interface : 1;
2061     unsigned int on_function : 1;
2062     unsigned int on_arg : 1;
2063     unsigned int on_type : 1;
2064     unsigned int on_enum : 1;
2065     unsigned int on_struct : 1;
2066     unsigned int on_union : 1;
2067     unsigned int on_field : 1;
2068     unsigned int on_library : 1;
2069     unsigned int on_dispinterface : 1;
2070     unsigned int on_module : 1;
2071     unsigned int on_coclass : 1;
2072     const char *display_name;
2073 };
2074
2075 struct allowed_attr allowed_attr[] =
2076 {
2077     /* attr                        { D ACF I Fn ARG T En St Un Fi  L  DI M  C  <display name> } */
2078     /* ATTR_AGGREGATABLE */        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2079     /* ATTR_APPOBJECT */           { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2080     /* ATTR_ASYNC */               { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
2081     /* ATTR_AUTO_HANDLE */         { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2082     /* ATTR_BINDABLE */            { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2083     /* ATTR_BROADCAST */           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2084     /* ATTR_CALLAS */              { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2085     /* ATTR_CALLCONV */            { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2086     /* ATTR_CASE */                { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2087     /* ATTR_CONST */               { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
2088     /* ATTR_CONTEXTHANDLE */       { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
2089     /* ATTR_CONTROL */             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2090     /* ATTR_DEFAULT */             { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
2091     /* ATTR_DEFAULTCOLLELEM */     { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2092     /* ATTR_DEFAULTVALUE */        { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2093     /* ATTR_DEFAULTVTABLE */       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2094     /* ATTR_DISPINTERFACE */       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2095     /* ATTR_DISPLAYBIND */         { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2096     /* ATTR_DLLNAME */             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2097     /* ATTR_DUAL */                { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2098     /* ATTR_ENDPOINT */            { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2099     /* ATTR_ENTRY */               { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2100     /* ATTR_EXPLICIT_HANDLE */     { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2101     /* ATTR_HANDLE */              { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2102     /* ATTR_HELPCONTEXT */         { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2103     /* ATTR_HELPFILE */            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2104     /* ATTR_HELPSTRING */          { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2105     /* ATTR_HELPSTRINGCONTEXT */   { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2106     /* ATTR_HELPSTRINGDLL */       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2107     /* ATTR_HIDDEN */              { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2108     /* ATTR_ID */                  { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
2109     /* ATTR_IDEMPOTENT */          { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2110     /* ATTR_IIDIS */               { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2111     /* ATTR_IMMEDIATEBIND */       { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2112     /* ATTR_IMPLICIT_HANDLE */     { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2113     /* ATTR_IN */                  { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2114     /* ATTR_INLINE */              { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2115     /* ATTR_INPUTSYNC */           { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2116     /* ATTR_LENGTHIS */            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2117     /* ATTR_LIBLCID */             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2118     /* ATTR_LOCAL */               { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2119     /* ATTR_NONBROWSABLE */        { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2120     /* ATTR_NONCREATABLE */        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2121     /* ATTR_NONEXTENSIBLE */       { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2122     /* ATTR_OBJECT */              { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2123     /* ATTR_ODL */                 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2124     /* ATTR_OLEAUTOMATION */       { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2125     /* ATTR_OPTIONAL */            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2126     /* ATTR_OUT */                 { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2127     /* ATTR_POINTERDEFAULT */      { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2128     /* ATTR_POINTERTYPE */         { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2129     /* ATTR_PROPGET */             { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2130     /* ATTR_PROPPUT */             { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2131     /* ATTR_PROPPUTREF */          { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2132     /* ATTR_PUBLIC */              { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2133     /* ATTR_RANGE */               { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2134     /* ATTR_READONLY */            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2135     /* ATTR_REQUESTEDIT */         { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2136     /* ATTR_RESTRICTED */          { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2137     /* ATTR_RETVAL */              { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2138     /* ATTR_SIZEIS */              { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2139     /* ATTR_SOURCE */              { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2140     /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2141     /* ATTR_STRING */              { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2142     /* ATTR_SWITCHIS */            { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2143     /* ATTR_SWITCHTYPE */          { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2144     /* ATTR_TRANSMITAS */          { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2145     /* ATTR_UUID */                { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
2146     /* ATTR_V1ENUM */              { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2147     /* ATTR_VARARG */              { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2148     /* ATTR_VERSION */             { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2149     /* ATTR_WIREMARSHAL */         { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2150 };
2151
2152 const char *get_attr_display_name(enum attr_type type)
2153 {
2154     return allowed_attr[type].display_name;
2155 }
2156
2157 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2158 {
2159   const attr_t *attr;
2160   if (!attrs) return attrs;
2161   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2162   {
2163     if (!allowed_attr[attr->type].on_interface)
2164       error_loc("inapplicable attribute %s for interface %s\n",
2165                 allowed_attr[attr->type].display_name, name);
2166   }
2167   return attrs;
2168 }
2169
2170 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2171 {
2172   const attr_t *attr;
2173   if (!attrs) return attrs;
2174   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2175   {
2176     if (!allowed_attr[attr->type].on_function)
2177       error_loc("inapplicable attribute %s for function %s\n",
2178                 allowed_attr[attr->type].display_name, name);
2179   }
2180   return attrs;
2181 }
2182
2183 static void check_arg(var_t *arg)
2184 {
2185   const type_t *t = arg->type;
2186   const attr_t *attr;
2187
2188   if (t->type == 0 && ! is_var_ptr(arg))
2189     error_loc("argument '%s' has void type\n", arg->name);
2190
2191   if (arg->attrs)
2192   {
2193     LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2194     {
2195       if (!allowed_attr[attr->type].on_arg)
2196         error_loc("inapplicable attribute %s for argument %s\n",
2197                   allowed_attr[attr->type].display_name, arg->name);
2198     }
2199   }
2200 }
2201
2202 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2203 {
2204   const attr_t *attr;
2205   if (!attrs) return attrs;
2206   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2207   {
2208     if (!allowed_attr[attr->type].on_type)
2209       error_loc("inapplicable attribute %s for typedef\n",
2210                 allowed_attr[attr->type].display_name);
2211   }
2212   return attrs;
2213 }
2214
2215 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2216 {
2217   const attr_t *attr;
2218   if (!attrs) return attrs;
2219   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2220   {
2221     if (!allowed_attr[attr->type].on_enum)
2222       error_loc("inapplicable attribute %s for enum\n",
2223                 allowed_attr[attr->type].display_name);
2224   }
2225   return attrs;
2226 }
2227
2228 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2229 {
2230   const attr_t *attr;
2231   if (!attrs) return attrs;
2232   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2233   {
2234     if (!allowed_attr[attr->type].on_struct)
2235       error_loc("inapplicable attribute %s for struct\n",
2236                 allowed_attr[attr->type].display_name);
2237   }
2238   return attrs;
2239 }
2240
2241 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2242 {
2243   const attr_t *attr;
2244   if (!attrs) return attrs;
2245   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2246   {
2247     if (!allowed_attr[attr->type].on_union)
2248       error_loc("inapplicable attribute %s for union\n",
2249                 allowed_attr[attr->type].display_name);
2250   }
2251   return attrs;
2252 }
2253
2254 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2255 {
2256   const attr_t *attr;
2257   if (!attrs) return attrs;
2258   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2259   {
2260     if (!allowed_attr[attr->type].on_field)
2261       error_loc("inapplicable attribute %s for field %s\n",
2262                 allowed_attr[attr->type].display_name, name);
2263   }
2264   return attrs;
2265 }
2266
2267 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2268 {
2269   const attr_t *attr;
2270   if (!attrs) return attrs;
2271   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2272   {
2273     if (!allowed_attr[attr->type].on_library)
2274       error_loc("inapplicable attribute %s for library %s\n",
2275                 allowed_attr[attr->type].display_name, name);
2276   }
2277   return attrs;
2278 }
2279
2280 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2281 {
2282   const attr_t *attr;
2283   if (!attrs) return attrs;
2284   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2285   {
2286     if (!allowed_attr[attr->type].on_dispinterface)
2287       error_loc("inapplicable attribute %s for dispinterface %s\n",
2288                 allowed_attr[attr->type].display_name, name);
2289   }
2290   return attrs;
2291 }
2292
2293 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2294 {
2295   const attr_t *attr;
2296   if (!attrs) return attrs;
2297   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2298   {
2299     if (!allowed_attr[attr->type].on_module)
2300       error_loc("inapplicable attribute %s for module %s\n",
2301                 allowed_attr[attr->type].display_name, name);
2302   }
2303   return attrs;
2304 }
2305
2306 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2307 {
2308   const attr_t *attr;
2309   if (!attrs) return attrs;
2310   LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2311   {
2312     if (!allowed_attr[attr->type].on_coclass)
2313       error_loc("inapplicable attribute %s for coclass %s\n",
2314                 allowed_attr[attr->type].display_name, name);
2315   }
2316   return attrs;
2317 }
2318
2319 static int is_allowed_conf_type(const type_t *type)
2320 {
2321     switch (type->type)
2322     {
2323     case RPC_FC_CHAR:
2324     case RPC_FC_SMALL:
2325     case RPC_FC_BYTE:
2326     case RPC_FC_USMALL:
2327     case RPC_FC_WCHAR:
2328     case RPC_FC_SHORT:
2329     case RPC_FC_ENUM16:
2330     case RPC_FC_USHORT:
2331     case RPC_FC_LONG:
2332     case RPC_FC_ENUM32:
2333     case RPC_FC_ULONG:
2334         return TRUE;
2335     default:
2336         return FALSE;
2337     }
2338 }
2339
2340 static int is_ptr_guid_type(const type_t *type)
2341 {
2342     unsigned int align = 0;
2343     for (;;)
2344     {
2345         if (type->kind == TKIND_ALIAS)
2346             type = type->orig;
2347         else if (is_ptr(type))
2348         {
2349             type = type->ref;
2350             break;
2351         }
2352         else
2353             return FALSE;
2354     }
2355     return (type_memsize(type, &align) == 16);
2356 }
2357
2358 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2359 {
2360     expr_t *dim;
2361     struct expr_loc expr_loc;
2362     expr_loc.v = arg;
2363     expr_loc.attr = attr_name;
2364     if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2365     {
2366         if (dim->type != EXPR_VOID)
2367         {
2368             const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2369             if (!is_allowed_conf_type(expr_type))
2370                 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2371                                attr_name);
2372         }
2373     }
2374 }
2375
2376 static void check_remoting_fields(const var_t *var, type_t *type);
2377
2378 /* checks that properties common to fields and arguments are consistent */
2379 static void check_field_common(const type_t *container_type,
2380                                const char *container_name, const var_t *arg)
2381 {
2382     type_t *type = arg->type;
2383     int is_wire_marshal = 0;
2384     int is_context_handle = 0;
2385     const char *container_type_name = NULL;
2386
2387     if (is_struct(container_type->type))
2388         container_type_name = "struct";
2389     else if (is_union(container_type->type))
2390         container_type_name = "union";
2391     else if (container_type->type == RPC_FC_FUNCTION)
2392         container_type_name = "function";
2393
2394     if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2395         (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2396         error_loc_info(&arg->loc_info,
2397                        "string and length_is specified for argument %s are mutually exclusive attributes\n",
2398                        arg->name);
2399
2400     if (is_attr(arg->attrs, ATTR_SIZEIS))
2401     {
2402         expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2403         check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2404     }
2405     if (is_attr(arg->attrs, ATTR_LENGTHIS))
2406     {
2407         expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2408         check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2409     }
2410     if (is_attr(arg->attrs, ATTR_IIDIS))
2411     {
2412         struct expr_loc expr_loc;
2413         expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2414         if (expr->type != EXPR_VOID)
2415         {
2416             const type_t *expr_type;
2417             expr_loc.v = arg;
2418             expr_loc.attr = "iid_is";
2419             expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2420             if (!expr_type || !is_ptr_guid_type(expr_type))
2421                 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2422         }
2423     }
2424     if (is_attr(arg->attrs, ATTR_SWITCHIS))
2425     {
2426         struct expr_loc expr_loc;
2427         expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2428         if (expr->type != EXPR_VOID)
2429         {
2430             const type_t *expr_type;
2431             expr_loc.v = arg;
2432             expr_loc.attr = "switch_is";
2433             expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2434             if (!expr_type || !is_allowed_conf_type(expr_type))
2435                 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2436                                expr_loc.attr);
2437         }
2438     }
2439
2440     /* get fundamental type for the argument */
2441     for (;;)
2442     {
2443         if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2444         {
2445             is_wire_marshal = 1;
2446             break;
2447         }
2448         if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2449         {
2450             is_context_handle = 1;
2451             break;
2452         }
2453         if (type->kind == TKIND_ALIAS)
2454             type = type->orig;
2455         else if (is_ptr(type) || is_array(type))
2456             type = type->ref;
2457         else
2458             break;
2459     }
2460
2461     if (type->type == 0 && !is_attr(arg->attrs, ATTR_IIDIS) && !is_wire_marshal && !is_context_handle)
2462         error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot derive from void *\n", arg->name, container_type_name, container_name);
2463     else if (type->type == RPC_FC_FUNCTION)
2464         error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot be a function pointer\n", arg->name, container_type_name, container_name);
2465     else if (!is_wire_marshal && (is_struct(type->type) || is_union(type->type)))
2466         check_remoting_fields(arg, type);
2467 }
2468
2469 static void check_remoting_fields(const var_t *var, type_t *type)
2470 {
2471     const var_t *field;
2472     const var_list_t *fields = NULL;
2473
2474     /* find the real type */
2475     while (type->kind == TKIND_ALIAS)
2476         type = type->orig;
2477
2478     if (type->checked)
2479         return;
2480
2481     type->checked = TRUE;
2482
2483     if (is_struct(type->type))
2484     {
2485         if (type->defined)
2486             fields = type->fields_or_args;
2487         else
2488             error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2489     }
2490     else if (is_union(type->type))
2491     {
2492         if (type->type == RPC_FC_ENCAPSULATED_UNION)
2493         {
2494             const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
2495             fields = uv->type->fields_or_args;
2496         }
2497         else
2498             fields = type->fields_or_args;
2499     }
2500
2501     if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2502         if (field->type) check_field_common(type, type->name, field);
2503 }
2504
2505 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2506 static void check_remoting_args(const func_t *func)
2507 {
2508     const char *funcname = func->def->name;
2509     const var_t *arg;
2510
2511     if (func->args) LIST_FOR_EACH_ENTRY( arg, func->args, const var_t, entry )
2512     {
2513         int ptr_level = 0;
2514         const type_t *type = arg->type;
2515
2516         /* get pointer level and fundamental type for the argument */
2517         for (;;)
2518         {
2519             if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2520                 break;
2521             if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2522                 break;
2523             if (type->kind == TKIND_ALIAS)
2524                 type = type->orig;
2525             else if (is_ptr(type))
2526             {
2527                 ptr_level++;
2528                 type = type->ref;
2529             }
2530             else
2531                 break;
2532         }
2533
2534         /* check that [out] parameters have enough pointer levels */
2535         if (is_attr(arg->attrs, ATTR_OUT))
2536         {
2537             if (!is_array(type))
2538             {
2539                 if (!ptr_level)
2540                     error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2541                 if (type->type == RPC_FC_IP && ptr_level == 1)
2542                     error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2543             }
2544         }
2545
2546         check_field_common(func->def->type, funcname, arg);
2547     }
2548 }
2549
2550 static void add_explicit_handle_if_necessary(func_t *func)
2551 {
2552     const var_t* explicit_handle_var;
2553     const var_t* explicit_generic_handle_var = NULL;
2554     const var_t* context_handle_var = NULL;
2555
2556     /* check for a defined binding handle */
2557     explicit_handle_var = get_explicit_handle_var(func);
2558     if (!explicit_handle_var)
2559     {
2560         explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2561         if (!explicit_generic_handle_var)
2562         {
2563             context_handle_var = get_context_handle_var(func);
2564             if (!context_handle_var)
2565             {
2566                 /* no explicit handle specified so add
2567                  * "[in] handle_t IDL_handle" as the first parameter to the
2568                  * function */
2569                 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2570                 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2571                 idl_handle->type = find_type_or_error("handle_t", 0);
2572                 if (!func->def->type->fields_or_args)
2573                 {
2574                     func->def->type->fields_or_args = xmalloc( sizeof(*func->def->type->fields_or_args) );
2575                     list_init( func->def->type->fields_or_args );
2576                 }
2577                 list_add_head( func->def->type->fields_or_args, &idl_handle->entry );
2578                 func->args = func->def->type->fields_or_args;
2579             }
2580         }
2581     }
2582 }
2583
2584 static void check_functions(const type_t *iface)
2585 {
2586     if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE) && iface->funcs)
2587     {
2588         func_t *func;
2589         LIST_FOR_EACH_ENTRY( func, iface->funcs, func_t, entry )
2590             add_explicit_handle_if_necessary(func);
2591     }
2592     if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2593     {
2594         const func_t *func;
2595         if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2596         {
2597             if (!is_attr(func->def->attrs, ATTR_LOCAL))
2598                 check_remoting_args(func);
2599         }
2600     }
2601 }
2602
2603 static void check_all_user_types(const statement_list_t *stmts)
2604 {
2605   const statement_t *stmt;
2606
2607   if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2608   {
2609     if (stmt->type == STMT_LIBRARY)
2610       check_all_user_types(stmt->u.lib->stmts);
2611     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
2612     {
2613       const func_t *f;
2614       const func_list_t *fs = stmt->u.type->funcs;
2615       if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2616         check_for_additional_prototype_types(f->args);
2617     }
2618   }
2619 }
2620
2621 int is_valid_uuid(const char *s)
2622 {
2623   int i;
2624
2625   for (i = 0; i < 36; ++i)
2626     if (i == 8 || i == 13 || i == 18 || i == 23)
2627     {
2628       if (s[i] != '-')
2629         return FALSE;
2630     }
2631     else
2632       if (!isxdigit(s[i]))
2633         return FALSE;
2634
2635   return s[i] == '\0';
2636 }
2637
2638 static statement_t *make_statement(enum statement_type type)
2639 {
2640     statement_t *stmt = xmalloc(sizeof(*stmt));
2641     stmt->type = type;
2642     return stmt;
2643 }
2644
2645 static statement_t *make_statement_type_decl(type_t *type)
2646 {
2647     statement_t *stmt = make_statement(STMT_TYPE);
2648     stmt->u.type = type;
2649     return stmt;
2650 }
2651
2652 static statement_t *make_statement_reference(type_t *type)
2653 {
2654     statement_t *stmt = make_statement(STMT_TYPEREF);
2655     stmt->u.type = type;
2656     return stmt;
2657 }
2658
2659 static statement_t *make_statement_declaration(var_t *var)
2660 {
2661     statement_t *stmt = make_statement(STMT_DECLARATION);
2662     stmt->u.var = var;
2663     if (var->stgclass == STG_EXTERN && var->eval)
2664         warning("'%s' initialised and declared extern\n", var->name);
2665     if (is_const_decl(var))
2666     {
2667         if (var->eval)
2668             reg_const(var);
2669     }
2670     else if ((var->stgclass == STG_NONE || var->stgclass == STG_REGISTER) &&
2671              var->type->type != RPC_FC_FUNCTION)
2672         error_loc("instantiation of data is illegal\n");
2673     return stmt;
2674 }
2675
2676 static statement_t *make_statement_library(typelib_t *typelib)
2677 {
2678     statement_t *stmt = make_statement(STMT_LIBRARY);
2679     stmt->u.lib = typelib;
2680     return stmt;
2681 }
2682
2683 static statement_t *make_statement_cppquote(const char *str)
2684 {
2685     statement_t *stmt = make_statement(STMT_CPPQUOTE);
2686     stmt->u.str = str;
2687     return stmt;
2688 }
2689
2690 static statement_t *make_statement_importlib(const char *str)
2691 {
2692     statement_t *stmt = make_statement(STMT_IMPORTLIB);
2693     stmt->u.str = str;
2694     return stmt;
2695 }
2696
2697 static statement_t *make_statement_import(const char *str)
2698 {
2699     statement_t *stmt = make_statement(STMT_IMPORT);
2700     stmt->u.str = str;
2701     return stmt;
2702 }
2703
2704 static statement_t *make_statement_module(type_t *type)
2705 {
2706     statement_t *stmt = make_statement(STMT_MODULE);
2707     stmt->u.type = type;
2708     return stmt;
2709 }
2710
2711 static statement_t *process_typedefs(declarator_list_t *decls)
2712 {
2713     declarator_t *decl, *next;
2714     statement_t *stmt;
2715     type_list_t **type_list;
2716
2717     if (!decls) return NULL;
2718
2719     stmt = make_statement(STMT_TYPEDEF);
2720     stmt->u.type_list = NULL;
2721     type_list = &stmt->u.type_list;
2722
2723     LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2724     {
2725         var_t *var = decl->var;
2726         type_t *type = find_type_or_error(var->name, 0);
2727         *type_list = xmalloc(sizeof(type_list_t));
2728         (*type_list)->type = type;
2729         (*type_list)->next = NULL;
2730
2731         if (! parse_only && do_header)
2732             write_typedef(type);
2733         if (in_typelib && is_attr(type->attrs, ATTR_PUBLIC))
2734             add_typelib_entry(type);
2735
2736         type_list = &(*type_list)->next;
2737         free(decl);
2738         free(var);
2739     }
2740
2741     return stmt;
2742 }
2743
2744 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2745 {
2746     if (!stmt) return list;
2747     if (!list)
2748     {
2749         list = xmalloc( sizeof(*list) );
2750         list_init( list );
2751     }
2752     list_add_tail( list, &stmt->entry );
2753     return list;
2754 }
2755
2756 static func_list_t *append_func_from_statement(func_list_t *list, statement_t *stmt)
2757 {
2758     if (stmt->type == STMT_DECLARATION)
2759     {
2760         var_t *var = stmt->u.var;
2761         if (var->stgclass == STG_NONE && var->type->type == RPC_FC_FUNCTION)
2762         {
2763             check_function_attrs(var->name, var->type->attrs);
2764             return append_func(list, make_func(stmt->u.var));
2765         }
2766     }
2767     return list;
2768 }
2769
2770 void init_loc_info(loc_info_t *i)
2771 {
2772     i->input_name = input_name ? input_name : "stdin";
2773     i->line_number = line_number;
2774     i->near_text = parser_text;
2775 }
2776
2777 static void check_def(const type_t *t)
2778 {
2779     if (t->defined)
2780         error_loc("%s: redefinition error; original definition was at %s:%d\n",
2781                   t->name, t->loc_info.input_name, t->loc_info.line_number);
2782 }