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