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