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