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