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