winepulse: Move session_wrapper_create into mmdevapi.
[wine.git] / tools / widl / parser.y
blobb802f75874dba604827a4382efd6a14530a33a06
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
6 * Copyright 2006-2008 Robert Shearman
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.
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.
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
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
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"
41 struct _import_t
43 char *name;
44 int import_performed;
47 static str_list_t *append_str(str_list_t *list, char *str);
48 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right,
49 enum storage_class stgclass, enum type_qualifier qual, enum function_specifier func_specifier);
50 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
51 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_t *decl, int top);
52 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
53 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
54 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
55 static declarator_t *make_declarator(var_t *var);
56 static type_t *make_safearray(type_t *type);
57 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
58 static void append_array(declarator_t *decl, expr_t *expr);
59 static void append_chain_type(declarator_t *decl, type_t *type, enum type_qualifier qual);
60 static void append_chain_callconv( struct location where, type_t *chain, char *callconv );
61 static warning_list_t *append_warning(warning_list_t *, int);
63 static type_t *reg_typedefs( struct location where, decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs );
64 static type_t *find_type_or_error(struct namespace *parent, const char *name);
65 static struct namespace *find_namespace_or_error(struct namespace *namespace, const char *name);
67 static var_t *reg_const(var_t *var);
69 static void push_namespaces(str_list_t *names);
70 static void pop_namespaces(str_list_t *names);
71 static void push_parameters_namespace(const char *name);
72 static void pop_parameters_namespace(const char *name);
74 static statement_list_t *append_parameterized_type_stmts(statement_list_t *stmts);
75 static void check_statements(const statement_list_t *stmts, int is_inside_library);
76 static void check_all_user_types(const statement_list_t *stmts);
77 static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func);
79 static void check_async_uuid(type_t *iface);
81 static statement_t *make_statement(enum statement_type type);
82 static statement_t *make_statement_type_decl(type_t *type);
83 static statement_t *make_statement_reference(type_t *type);
84 static statement_t *make_statement_declaration(var_t *var);
85 static statement_t *make_statement_library(typelib_t *typelib);
86 static statement_t *make_statement_pragma(const char *str);
87 static statement_t *make_statement_cppquote(const char *str);
88 static statement_t *make_statement_importlib(const char *str);
89 static statement_t *make_statement_module(type_t *type);
90 static statement_t *make_statement_typedef(var_list_t *names, int declonly);
91 static statement_t *make_statement_import(const char *str);
92 static statement_t *make_statement_parameterized_type(type_t *type, typeref_list_t *params);
93 static statement_t *make_statement_delegate(type_t *ret, var_list_t *args);
94 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
95 static statement_list_t *append_statements(statement_list_t *, statement_list_t *);
97 static struct namespace global_namespace = {
98 NULL, NULL, LIST_INIT(global_namespace.entry), LIST_INIT(global_namespace.children)
101 static struct namespace *current_namespace = &global_namespace;
102 static struct namespace *parameters_namespace = NULL;
103 static statement_list_t *parameterized_type_stmts = NULL;
105 static typelib_t *current_typelib;
109 %code requires
112 #define PARSER_LTYPE struct location
116 %code provides
119 int parser_lex( PARSER_STYPE *yylval, PARSER_LTYPE *yylloc );
120 void push_import( const char *fname, PARSER_LTYPE *yylloc );
121 void pop_import( PARSER_LTYPE *yylloc );
123 # define YYLLOC_DEFAULT( cur, rhs, n ) \
124 do { if (n) init_location( &(cur), &YYRHSLOC( rhs, 1 ), &YYRHSLOC( rhs, n ) ); \
125 else init_location( &(cur), &YYRHSLOC( rhs, 0 ), NULL ); } while(0)
129 %define api.prefix {parser_}
130 %define api.pure full
131 %define parse.error verbose
132 %locations
134 %union {
135 attr_t *attr;
136 attr_list_t *attr_list;
137 str_list_t *str_list;
138 expr_t *expr;
139 expr_list_t *expr_list;
140 type_t *type;
141 var_t *var;
142 var_list_t *var_list;
143 declarator_t *declarator;
144 declarator_list_t *declarator_list;
145 statement_t *statement;
146 statement_list_t *stmt_list;
147 warning_t *warning;
148 warning_list_t *warning_list;
149 typeref_t *typeref;
150 typeref_list_t *typeref_list;
151 char *str;
152 struct uuid *uuid;
153 unsigned int num;
154 double dbl;
155 typelib_t *typelib;
156 struct _import_t *import;
157 struct _decl_spec_t *declspec;
158 enum storage_class stgclass;
159 enum type_qualifier type_qualifier;
160 enum function_specifier function_specifier;
161 struct namespace *namespace;
164 %token <str> aIDENTIFIER aPRAGMA
165 %token <str> aKNOWNTYPE
166 %token <num> aNUM aHEXNUM
167 %token <dbl> aDOUBLE
168 %token <str> aSTRING aWSTRING aSQSTRING
169 %token <str> tCDECL
170 %token <str> tFASTCALL
171 %token <str> tPASCAL
172 %token <str> tSTDCALL
173 %token <uuid> aUUID
174 %token aEOF aACF
175 %token SHL SHR
176 %token MEMBERPTR
177 %token EQUALITY INEQUALITY
178 %token GREATEREQUAL LESSEQUAL
179 %token LOGICALOR LOGICALAND
180 %token ELLIPSIS
181 %token tACTIVATABLE
182 %token tAGGREGATABLE
183 %token tAGILE
184 %token tALLNODES tALLOCATE tANNOTATION
185 %token tAPICONTRACT
186 %token tAPPOBJECT tASYNC tASYNCUUID
187 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
188 %token tCALLAS tCALLBACK tCASE tCHAR tCOCLASS tCODE tCOMMSTATUS
189 %token tCOMPOSABLE
190 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
191 %token tCONTEXTHANDLESERIALIZE
192 %token tCONTRACT
193 %token tCONTRACTVERSION
194 %token tCONTROL tCPPQUOTE
195 %token tCUSTOM
196 %token tDECLARE
197 %token tDECODE tDEFAULT tDEFAULTBIND
198 %token tDELEGATE
199 %token tDEFAULTCOLLELEM
200 %token tDEFAULTVALUE
201 %token tDEFAULTVTABLE
202 %token tDISABLECONSISTENCYCHECK tDISPLAYBIND
203 %token tDISPINTERFACE
204 %token tDLLNAME tDONTFREE tDOUBLE tDUAL
205 %token tENABLEALLOCATE tENCODE tENDPOINT
206 %token tENTRY tENUM tERRORSTATUST
207 %token tEVENTADD tEVENTREMOVE
208 %token tEXCLUSIVETO
209 %token tEXPLICITHANDLE tEXTERN
210 %token tFALSE
211 %token tFAULTSTATUS
212 %token tFLAGS
213 %token tFLOAT tFORCEALLOCATE
214 %token tHANDLE
215 %token tHANDLET
216 %token tHELPCONTEXT tHELPFILE
217 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
218 %token tHIDDEN
219 %token tHYPER tID tIDEMPOTENT
220 %token tIGNORE tIIDIS
221 %token tIMMEDIATEBIND
222 %token tIMPLICITHANDLE
223 %token tIMPORT tIMPORTLIB
224 %token tIN tIN_LINE tINLINE
225 %token tINPUTSYNC
226 %token tINT tINT32 tINT3264 tINT64
227 %token tINTERFACE
228 %token tLCID
229 %token tLENGTHIS tLIBRARY
230 %token tLICENSED tLOCAL
231 %token tLONG
232 %token tMARSHALINGBEHAVIOR
233 %token tMAYBE tMESSAGE
234 %token tMETHODS
235 %token tMODULE
236 %token tMTA
237 %token tNAMESPACE
238 %token tNOCODE tNONBROWSABLE
239 %token tNONCREATABLE
240 %token tNONE
241 %token tNONEXTENSIBLE
242 %token tNOTIFY tNOTIFYFLAG
243 %token tNULL
244 %token tOBJECT tODL tOLEAUTOMATION
245 %token tOPTIMIZE tOPTIONAL
246 %token tOUT
247 %token tOVERLOAD
248 %token tPARTIALIGNORE
249 %token tPOINTERDEFAULT
250 %token tPRAGMA_WARNING
251 %token tPROGID tPROPERTIES
252 %token tPROPGET tPROPPUT tPROPPUTREF
253 %token tPROTECTED
254 %token tPROXY tPTR
255 %token tPUBLIC
256 %token tRANGE
257 %token tREADONLY tREF
258 %token tREGISTER tREPRESENTAS
259 %token tREQUESTEDIT
260 %token tREQUIRES
261 %token tRESTRICTED
262 %token tRETVAL
263 %token tRUNTIMECLASS
264 %token tSAFEARRAY
265 %token tSHORT
266 %token tSIGNED tSINGLENODE
267 %token tSIZEIS tSIZEOF
268 %token tSMALL
269 %token tSOURCE
270 %token tSTANDARD
271 %token tSTATIC
272 %token tSTRICTCONTEXTHANDLE
273 %token tSTRING tSTRUCT
274 %token tSWITCH tSWITCHIS tSWITCHTYPE
275 %token tTHREADING tTRANSMITAS
276 %token tTRUE
277 %token tTYPEDEF
278 %token tUIDEFAULT tUNION
279 %token tUNIQUE
280 %token tUNSIGNED
281 %token tUSESGETLASTERROR tUSERMARSHAL tUUID
282 %token tV1ENUM
283 %token tVARARG
284 %token tVERSION tVIPROGID
285 %token tVOID
286 %token tWCHAR tWIREMARSHAL
287 %token tAPARTMENT tNEUTRAL tSINGLE tFREE tBOTH
289 %type <attr> access_attr
290 %type <attr> attribute acf_attribute
291 %type <attr_list> m_attributes attributes attrib_list
292 %type <attr_list> acf_attributes acf_attribute_list
293 %type <attr_list> dispattributes
294 %type <str_list> str_list
295 %type <expr> m_expr expr expr_const expr_int_const array m_bitfield
296 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
297 %type <expr> contract_req
298 %type <expr> static_attr
299 %type <expr> activatable_attr
300 %type <expr> composable_attr
301 %type <type> delegatedef
302 %type <stgclass> storage_cls_spec
303 %type <type_qualifier> type_qualifier m_type_qual_list
304 %type <function_specifier> function_specifier
305 %type <declspec> decl_spec unqualified_decl_spec decl_spec_no_type m_decl_spec_no_type
306 %type <type> inherit interface interfacedef
307 %type <type> interfaceref
308 %type <type> dispinterfaceref
309 %type <type> dispinterface dispinterfacedef
310 %type <type> module moduledef
311 %type <str_list> namespacedef
312 %type <type> base_type int_std
313 %type <type> enumdef structdef uniondef typedecl
314 %type <type> type unqualified_type qualified_type
315 %type <type> type_parameter
316 %type <typeref_list> type_parameters
317 %type <type> parameterized_type
318 %type <type> parameterized_type_arg
319 %type <typeref_list> parameterized_type_args
320 %type <typeref> class_interface
321 %type <typeref_list> class_interfaces
322 %type <typeref_list> requires required_types
323 %type <var> arg ne_union_field union_field s_field case enum enum_member declaration
324 %type <var> funcdef
325 %type <var_list> m_args arg_list args dispint_meths
326 %type <var_list> fields ne_union_fields cases enums enum_list dispint_props field
327 %type <var> m_ident ident
328 %type <declarator> declarator direct_declarator init_declarator struct_declarator
329 %type <declarator> m_any_declarator any_declarator any_declarator_no_direct any_direct_declarator
330 %type <declarator> m_abstract_declarator abstract_declarator abstract_declarator_no_direct abstract_direct_declarator
331 %type <declarator_list> declarator_list struct_declarator_list
332 %type <type> coclass coclassdef
333 %type <type> runtimeclass runtimeclass_def
334 %type <type> apicontract apicontract_def
335 %type <num> contract_ver
336 %type <num> pointer_type threading_type marshaling_behavior version
337 %type <str> libraryhdr callconv cppquote importlib import
338 %type <str> typename m_typename
339 %type <str> import_start
340 %type <typelib> library_start librarydef
341 %type <statement> statement typedef pragma_warning
342 %type <stmt_list> gbl_statements imp_statements int_statements
343 %type <stmt_list> decl_block decl_statements
344 %type <stmt_list> imp_decl_block imp_decl_statements
345 %type <warning_list> warnings
346 %type <num> allocate_option_list allocate_option
347 %type <namespace> namespace_pfx
349 %left ','
350 %right '?' ':'
351 %left LOGICALOR
352 %left LOGICALAND
353 %left '|'
354 %left '^'
355 %left '&'
356 %left EQUALITY INEQUALITY
357 %left '<' '>' LESSEQUAL GREATEREQUAL
358 %left SHL SHR
359 %left '-' '+'
360 %left '*' '/' '%'
361 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
362 %left '.' MEMBERPTR '[' ']'
366 input: gbl_statements m_acf { $1 = append_parameterized_type_stmts($1);
367 check_statements($1, FALSE);
368 check_all_user_types($1);
369 write_header($1);
370 write_id_data($1);
371 write_proxies($1);
372 write_client($1);
373 write_server($1);
374 write_regscript($1);
375 write_typelib_regscript($1);
376 write_dlldata($1);
377 write_local_stubs($1);
381 m_acf
382 : %empty
383 | aACF acf_statements
386 decl_statements
387 : %empty { $$ = NULL; }
388 | decl_statements tINTERFACE qualified_type '<' parameterized_type_args '>' ';'
389 { parameterized_type_stmts = append_statement(parameterized_type_stmts, make_statement_parameterized_type($3, $5));
390 $$ = append_statement($1, make_statement_reference(type_parameterized_type_specialize_declare($3, $5)));
394 decl_block: tDECLARE '{' decl_statements '}' { $$ = $3; }
397 imp_decl_statements
398 : %empty { $$ = NULL; }
399 | imp_decl_statements tINTERFACE qualified_type '<' parameterized_type_args '>' ';'
400 { $$ = append_statement($1, make_statement_reference(type_parameterized_type_specialize_declare($3, $5))); }
403 imp_decl_block
404 : tDECLARE '{' imp_decl_statements '}' { $$ = $3; }
407 gbl_statements
408 : %empty { $$ = NULL; }
409 | gbl_statements namespacedef '{' { push_namespaces($2); } gbl_statements '}'
410 { pop_namespaces($2); $$ = append_statements($1, $5); }
411 | gbl_statements interface ';' { $$ = append_statement($1, make_statement_reference($2)); }
412 | gbl_statements dispinterface ';' { $$ = append_statement($1, make_statement_reference($2)); }
413 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
414 | gbl_statements delegatedef { $$ = append_statement($1, make_statement_type_decl($2)); }
415 | gbl_statements coclass ';' { $$ = $1;
416 reg_type($2, $2->name, current_namespace, 0);
418 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
419 reg_type($2, $2->name, current_namespace, 0);
421 | gbl_statements apicontract ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
422 | gbl_statements apicontract_def { $$ = append_statement($1, make_statement_type_decl($2));
423 reg_type($2, $2->name, current_namespace, 0); }
424 | gbl_statements runtimeclass ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
425 | gbl_statements runtimeclass_def { $$ = append_statement($1, make_statement_type_decl($2));
426 reg_type($2, $2->name, current_namespace, 0); }
427 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
428 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
429 | gbl_statements statement { $$ = append_statement($1, $2); }
430 | gbl_statements decl_block { $$ = append_statements($1, $2); }
433 imp_statements
434 : %empty { $$ = NULL; }
435 | imp_statements interface ';' { $$ = append_statement($1, make_statement_reference($2)); }
436 | imp_statements dispinterface ';' { $$ = append_statement($1, make_statement_reference($2)); }
437 | imp_statements namespacedef '{' { push_namespaces($2); } imp_statements '}'
438 { pop_namespaces($2); $$ = append_statements($1, $5); }
439 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
440 | imp_statements delegatedef { $$ = append_statement($1, make_statement_type_decl($2)); }
441 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
442 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
443 reg_type($2, $2->name, current_namespace, 0);
445 | imp_statements apicontract ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
446 | imp_statements apicontract_def { $$ = append_statement($1, make_statement_type_decl($2));
447 reg_type($2, $2->name, current_namespace, 0); }
448 | imp_statements runtimeclass ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
449 | imp_statements runtimeclass_def { $$ = append_statement($1, make_statement_type_decl($2));
450 reg_type($2, $2->name, current_namespace, 0); }
451 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
452 | imp_statements statement { $$ = append_statement($1, $2); }
453 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
454 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
455 | imp_statements imp_decl_block { $$ = append_statements($1, $2); }
458 int_statements
459 : %empty { $$ = NULL; }
460 | int_statements statement { $$ = append_statement($1, $2); }
463 semicolon_opt
464 : %empty
465 | ';'
468 statement:
469 cppquote { $$ = make_statement_cppquote($1); }
470 | typedecl ';' { $$ = make_statement_type_decl($1); }
471 | declaration ';' { $$ = make_statement_declaration($1); }
472 | import { $$ = make_statement_import($1); }
473 | typedef ';' { $$ = $1; }
474 | aPRAGMA { $$ = make_statement_pragma($1); }
475 | pragma_warning { $$ = NULL; }
478 pragma_warning: tPRAGMA_WARNING '(' aIDENTIFIER ':' warnings ')'
480 int result;
481 $$ = NULL;
482 result = do_warning($3, $5);
483 if(!result)
484 error_loc("expected \"disable\", \"enable\" or \"default\"\n");
486 | tPRAGMA_WARNING '(' tDEFAULT ':' warnings ')'
488 $$ = NULL;
489 do_warning("default", $5);
493 warnings:
494 aNUM { $$ = append_warning(NULL, $1); }
495 | warnings aNUM { $$ = append_warning($1, $2); }
498 typedecl:
499 enumdef
500 | tENUM aIDENTIFIER { $$ = type_new_enum($2, current_namespace, FALSE, NULL); }
501 | structdef
502 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, current_namespace, FALSE, NULL); }
503 | uniondef
504 | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, current_namespace, FALSE, NULL); }
505 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
506 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
507 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
510 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
513 import_start: tIMPORT aSTRING ';' { $$ = $2; push_import( $2, &yylloc ); }
515 import: import_start imp_statements aEOF { pop_import( &yylloc ); }
518 importlib: tIMPORTLIB '(' aSTRING ')'
519 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3, current_typelib); }
522 libraryhdr: tLIBRARY typename { $$ = $2; }
524 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
525 if (!parse_only && do_typelib) current_typelib = $$;
528 librarydef: library_start imp_statements '}'
529 semicolon_opt { $$ = $1; $$->stmts = $2; }
532 m_args
533 : %empty { $$ = NULL; }
534 | args
537 arg_list: arg { check_arg_attrs($1); $$ = append_var( NULL, $1 ); }
538 | arg_list ',' arg { check_arg_attrs($3); $$ = append_var( $1, $3 ); }
541 args: arg_list
542 | arg_list ',' ELLIPSIS { $$ = append_var( $1, make_var(xstrdup("...")) ); }
545 /* split into two rules to get bison to resolve a tVOID conflict */
546 arg: attributes decl_spec m_any_declarator { if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
547 error_loc("invalid storage class for function parameter\n");
548 $$ = declare_var($1, $2, $3, TRUE);
549 free($2); free($3);
551 | decl_spec m_any_declarator { if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
552 error_loc("invalid storage class for function parameter\n");
553 $$ = declare_var(NULL, $1, $2, TRUE);
554 free($1); free($2);
558 array: '[' expr ']' { $$ = $2;
559 if (!$$->is_const || $$->cval <= 0)
560 error_loc("array dimension is not a positive integer constant\n");
562 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
563 | '[' ']' { $$ = make_expr(EXPR_VOID); }
566 m_attributes
567 : %empty { $$ = NULL; }
568 | attributes
571 attributes:
572 '[' attrib_list ']' { $$ = $2; }
575 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
576 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
577 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
580 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
581 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
584 marshaling_behavior:
585 tAGILE { $$ = MARSHALING_AGILE; }
586 | tNONE { $$ = MARSHALING_NONE; }
587 | tSTANDARD { $$ = MARSHALING_STANDARD; }
590 contract_ver:
591 aNUM { $$ = MAKEVERSION(0, $1); }
592 | aNUM '.' aNUM { $$ = MAKEVERSION($3, $1); }
595 contract_req
596 : decl_spec ',' contract_ver { if ($1->type->type_type != TYPE_APICONTRACT)
597 error_loc("type %s is not an apicontract\n", $1->type->name);
598 $$ = make_exprl(EXPR_NUM, $3);
599 $$ = make_exprt(EXPR_GTREQL, declare_var(NULL, $1, make_declarator(NULL), 0), $$);
603 static_attr
604 : decl_spec ',' contract_req { if ($1->type->type_type != TYPE_INTERFACE)
605 error_loc("type %s is not an interface\n", $1->type->name);
606 $$ = make_exprt(EXPR_MEMBER, declare_var(NULL, $1, make_declarator(NULL), 0), $3);
610 activatable_attr:
611 decl_spec ',' contract_req { if ($1->type->type_type != TYPE_INTERFACE)
612 error_loc("type %s is not an interface\n", $1->type->name);
613 $$ = make_exprt(EXPR_MEMBER, declare_var(NULL, $1, make_declarator(NULL), 0), $3);
615 | contract_req { $$ = $1; } /* activatable on the default activation factory */
618 access_attr
619 : tPUBLIC { $$ = attr_int( @$, ATTR_PUBLIC, 0 ); }
620 | tPROTECTED { $$ = attr_int( @$, ATTR_PROTECTED, 0 ); }
623 composable_attr
624 : decl_spec ',' access_attr ',' contract_req
625 { if ($1->type->type_type != TYPE_INTERFACE)
626 error_loc( "type %s is not an interface\n", $1->type->name );
627 $$ = make_exprt( EXPR_MEMBER, declare_var( append_attr( NULL, $3 ), $1, make_declarator( NULL ), 0 ), $5 );
631 attribute
632 : %empty { $$ = NULL; }
633 | tACTIVATABLE '(' activatable_attr ')' { $$ = attr_ptr( @$, ATTR_ACTIVATABLE, $3 ); }
634 | tAGGREGATABLE { $$ = attr_int( @$, ATTR_AGGREGATABLE, 0 ); }
635 | tANNOTATION '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_ANNOTATION, $3 ); }
636 | tAPPOBJECT { $$ = attr_int( @$, ATTR_APPOBJECT, 0 ); }
637 | tASYNC { $$ = attr_int( @$, ATTR_ASYNC, 0 ); }
638 | tAUTOHANDLE { $$ = attr_int( @$, ATTR_AUTO_HANDLE, 0 ); }
639 | tBINDABLE { $$ = attr_int( @$, ATTR_BINDABLE, 0 ); }
640 | tBROADCAST { $$ = attr_int( @$, ATTR_BROADCAST, 0 ); }
641 | tCALLAS '(' ident ')' { $$ = attr_ptr( @$, ATTR_CALLAS, $3 ); }
642 | tCASE '(' expr_list_int_const ')' { $$ = attr_ptr( @$, ATTR_CASE, $3 ); }
643 | tCODE { $$ = attr_int( @$, ATTR_CODE, 0 ); }
644 | tCOMPOSABLE '(' composable_attr ')' { $$ = attr_ptr( @$, ATTR_COMPOSABLE, $3 ); }
645 | tCOMMSTATUS { $$ = attr_int( @$, ATTR_COMMSTATUS, 0 ); }
646 | tCONTEXTHANDLE { $$ = attr_int( @$, ATTR_CONTEXTHANDLE, 0 ); }
647 | tCONTEXTHANDLENOSERIALIZE { $$ = attr_int( @$, ATTR_CONTEXTHANDLE, 0 ); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
648 | tCONTEXTHANDLESERIALIZE { $$ = attr_int( @$, ATTR_CONTEXTHANDLE, 0 ); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
649 | tCONTRACT '(' contract_req ')' { $$ = attr_ptr( @$, ATTR_CONTRACT, $3 ); }
650 | tCONTRACTVERSION '(' contract_ver ')' { $$ = attr_int( @$, ATTR_CONTRACTVERSION, $3 ); }
651 | tCONTROL { $$ = attr_int( @$, ATTR_CONTROL, 0 ); }
652 | tCUSTOM '(' aUUID ',' expr_const ')' { attr_custdata_t *data = xmalloc( sizeof(*data) );
653 data->id = *$3; data->pval = $5;
654 $$ = attr_ptr( @$, ATTR_CUSTOM, data );
656 | tDECODE { $$ = attr_int( @$, ATTR_DECODE, 0 ); }
657 | tDEFAULT { $$ = attr_int( @$, ATTR_DEFAULT, 0 ); }
658 | tDEFAULTBIND { $$ = attr_int( @$, ATTR_DEFAULTBIND, 0 ); }
659 | tDEFAULTCOLLELEM { $$ = attr_int( @$, ATTR_DEFAULTCOLLELEM, 0 ); }
660 | tDEFAULTVALUE '(' expr_const ')' { $$ = attr_ptr( @$, ATTR_DEFAULTVALUE, $3 ); }
661 | tDEFAULTVTABLE { $$ = attr_int( @$, ATTR_DEFAULTVTABLE, 0 ); }
662 | tDISABLECONSISTENCYCHECK { $$ = attr_int( @$, ATTR_DISABLECONSISTENCYCHECK, 0 ); }
663 | tDISPLAYBIND { $$ = attr_int( @$, ATTR_DISPLAYBIND, 0 ); }
664 | tDLLNAME '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_DLLNAME, $3 ); }
665 | tDUAL { $$ = attr_int( @$, ATTR_DUAL, 0 ); }
666 | tENABLEALLOCATE { $$ = attr_int( @$, ATTR_ENABLEALLOCATE, 0 ); }
667 | tENCODE { $$ = attr_int( @$, ATTR_ENCODE, 0 ); }
668 | tENDPOINT '(' str_list ')' { $$ = attr_ptr( @$, ATTR_ENDPOINT, $3 ); }
669 | tENTRY '(' expr_const ')' { $$ = attr_ptr( @$, ATTR_ENTRY, $3 ); }
670 | tEVENTADD { $$ = attr_int( @$, ATTR_EVENTADD, 0 ); }
671 | tEVENTREMOVE { $$ = attr_int( @$, ATTR_EVENTREMOVE, 0 ); }
672 | tEXCLUSIVETO '(' decl_spec ')' { if ($3->type->type_type != TYPE_RUNTIMECLASS)
673 error_loc( "type %s is not a runtimeclass\n", $3->type->name );
674 $$ = attr_ptr( @$, ATTR_EXCLUSIVETO, $3->type );
676 | tEXPLICITHANDLE { $$ = attr_int( @$, ATTR_EXPLICIT_HANDLE, 0 ); }
677 | tFAULTSTATUS { $$ = attr_int( @$, ATTR_FAULTSTATUS, 0 ); }
678 | tFLAGS { $$ = attr_int( @$, ATTR_FLAGS, 0 ); }
679 | tFORCEALLOCATE { $$ = attr_int( @$, ATTR_FORCEALLOCATE, 0 ); }
680 | tHANDLE { $$ = attr_int( @$, ATTR_HANDLE, 0 ); }
681 | tHELPCONTEXT '(' expr_int_const ')' { $$ = attr_ptr( @$, ATTR_HELPCONTEXT, $3 ); }
682 | tHELPFILE '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_HELPFILE, $3 ); }
683 | tHELPSTRING '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_HELPSTRING, $3 ); }
684 | tHELPSTRINGCONTEXT '(' expr_int_const ')'
685 { $$ = attr_ptr( @$, ATTR_HELPSTRINGCONTEXT, $3 ); }
686 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_HELPSTRINGDLL, $3 ); }
687 | tHIDDEN { $$ = attr_int( @$, ATTR_HIDDEN, 0 ); }
688 | tID '(' expr_int_const ')' { $$ = attr_ptr( @$, ATTR_ID, $3 ); }
689 | tIDEMPOTENT { $$ = attr_int( @$, ATTR_IDEMPOTENT, 0 ); }
690 | tIGNORE { $$ = attr_int( @$, ATTR_IGNORE, 0 ); }
691 | tIIDIS '(' expr ')' { $$ = attr_ptr( @$, ATTR_IIDIS, $3 ); }
692 | tIMMEDIATEBIND { $$ = attr_int( @$, ATTR_IMMEDIATEBIND, 0 ); }
693 | tIMPLICITHANDLE '(' arg ')' { $$ = attr_ptr( @$, ATTR_IMPLICIT_HANDLE, $3 ); }
694 | tIN { $$ = attr_int( @$, ATTR_IN, 0 ); }
695 | tINPUTSYNC { $$ = attr_int( @$, ATTR_INPUTSYNC, 0 ); }
696 | tLENGTHIS '(' m_exprs ')' { $$ = attr_ptr( @$, ATTR_LENGTHIS, $3 ); }
697 | tLCID '(' expr_int_const ')' { $$ = attr_ptr( @$, ATTR_LIBLCID, $3 ); }
698 | tLCID { $$ = attr_int( @$, ATTR_PARAMLCID, 0 ); }
699 | tLICENSED { $$ = attr_int( @$, ATTR_LICENSED, 0 ); }
700 | tLOCAL { $$ = attr_int( @$, ATTR_LOCAL, 0 ); }
701 | tMARSHALINGBEHAVIOR '(' marshaling_behavior ')'
702 { $$ = attr_int( @$, ATTR_MARSHALING_BEHAVIOR, $3 ); }
703 | tMAYBE { $$ = attr_int( @$, ATTR_MAYBE, 0 ); }
704 | tMESSAGE { $$ = attr_int( @$, ATTR_MESSAGE, 0 ); }
705 | tNOCODE { $$ = attr_int( @$, ATTR_NOCODE, 0 ); }
706 | tNONBROWSABLE { $$ = attr_int( @$, ATTR_NONBROWSABLE, 0 ); }
707 | tNONCREATABLE { $$ = attr_int( @$, ATTR_NONCREATABLE, 0 ); }
708 | tNONEXTENSIBLE { $$ = attr_int( @$, ATTR_NONEXTENSIBLE, 0 ); }
709 | tNOTIFY { $$ = attr_int( @$, ATTR_NOTIFY, 0 ); }
710 | tNOTIFYFLAG { $$ = attr_int( @$, ATTR_NOTIFYFLAG, 0 ); }
711 | tOBJECT { $$ = attr_int( @$, ATTR_OBJECT, 0 ); }
712 | tODL { $$ = attr_int( @$, ATTR_ODL, 0 ); }
713 | tOLEAUTOMATION { $$ = attr_int( @$, ATTR_OLEAUTOMATION, 0 ); }
714 | tOPTIMIZE '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_OPTIMIZE, $3 ); }
715 | tOPTIONAL { $$ = attr_int( @$, ATTR_OPTIONAL, 0 ); }
716 | tOUT { $$ = attr_int( @$, ATTR_OUT, 0 ); }
717 | tOVERLOAD '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_OVERLOAD, $3 ); }
718 | tPARTIALIGNORE { $$ = attr_int( @$, ATTR_PARTIALIGNORE, 0 ); }
719 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = attr_int( @$, ATTR_POINTERDEFAULT, $3 ); }
720 | tPROGID '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_PROGID, $3 ); }
721 | tPROPGET { $$ = attr_int( @$, ATTR_PROPGET, 0 ); }
722 | tPROPPUT { $$ = attr_int( @$, ATTR_PROPPUT, 0 ); }
723 | tPROPPUTREF { $$ = attr_int( @$, ATTR_PROPPUTREF, 0 ); }
724 | tPROTECTED { $$ = attr_int( @$, ATTR_PROTECTED, 0 ); }
725 | tPROXY { $$ = attr_int( @$, ATTR_PROXY, 0 ); }
726 | tPUBLIC { $$ = attr_int( @$, ATTR_PUBLIC, 0 ); }
727 | tRANGE '(' expr_int_const ',' expr_int_const ')'
728 { expr_list_t *list = append_expr( NULL, $3 );
729 list = append_expr( list, $5 );
730 $$ = attr_ptr( @$, ATTR_RANGE, list );
732 | tREADONLY { $$ = attr_int( @$, ATTR_READONLY, 0 ); }
733 | tREPRESENTAS '(' type ')' { $$ = attr_ptr( @$, ATTR_REPRESENTAS, $3 ); }
734 | tREQUESTEDIT { $$ = attr_int( @$, ATTR_REQUESTEDIT, 0 ); }
735 | tRESTRICTED { $$ = attr_int( @$, ATTR_RESTRICTED, 0 ); }
736 | tRETVAL { $$ = attr_int( @$, ATTR_RETVAL, 0 ); }
737 | tSIZEIS '(' m_exprs ')' { $$ = attr_ptr( @$, ATTR_SIZEIS, $3 ); }
738 | tSOURCE { $$ = attr_int( @$, ATTR_SOURCE, 0 ); }
739 | tSTATIC '(' static_attr ')' { $$ = attr_ptr( @$, ATTR_STATIC, $3 ); }
740 | tSTRICTCONTEXTHANDLE { $$ = attr_int( @$, ATTR_STRICTCONTEXTHANDLE, 0 ); }
741 | tSTRING { $$ = attr_int( @$, ATTR_STRING, 0 ); }
742 | tSWITCHIS '(' expr ')' { $$ = attr_ptr( @$, ATTR_SWITCHIS, $3 ); }
743 | tSWITCHTYPE '(' type ')' { $$ = attr_ptr( @$, ATTR_SWITCHTYPE, $3 ); }
744 | tTRANSMITAS '(' type ')' { $$ = attr_ptr( @$, ATTR_TRANSMITAS, $3 ); }
745 | tTHREADING '(' threading_type ')' { $$ = attr_int( @$, ATTR_THREADING, $3 ); }
746 | tUIDEFAULT { $$ = attr_int( @$, ATTR_UIDEFAULT, 0 ); }
747 | tUSESGETLASTERROR { $$ = attr_int( @$, ATTR_USESGETLASTERROR, 0 ); }
748 | tUSERMARSHAL '(' type ')' { $$ = attr_ptr( @$, ATTR_USERMARSHAL, $3 ); }
749 | tUUID '(' aUUID ')' { $$ = attr_ptr( @$, ATTR_UUID, $3 ); }
750 | tASYNCUUID '(' aUUID ')' { $$ = attr_ptr( @$, ATTR_ASYNCUUID, $3 ); }
751 | tV1ENUM { $$ = attr_int( @$, ATTR_V1ENUM, 0 ); }
752 | tVARARG { $$ = attr_int( @$, ATTR_VARARG, 0 ); }
753 | tVERSION '(' version ')' { $$ = attr_int( @$, ATTR_VERSION, $3 ); }
754 | tVIPROGID '(' aSTRING ')' { $$ = attr_ptr( @$, ATTR_VIPROGID, $3 ); }
755 | tWIREMARSHAL '(' type ')' { $$ = attr_ptr( @$, ATTR_WIREMARSHAL, $3 ); }
756 | pointer_type { $$ = attr_int( @$, ATTR_POINTERTYPE, $1 ); }
759 callconv: tCDECL
760 | tFASTCALL
761 | tPASCAL
762 | tSTDCALL
765 cases
766 : %empty { $$ = NULL; }
767 | cases case { $$ = append_var( $1, $2 ); }
770 case : tCASE expr_int_const ':' union_field { attr_t *a = attr_ptr( @$, ATTR_CASE, append_expr( NULL, $2 ) );
771 $$ = $4; if (!$$) $$ = make_var( NULL );
772 $$->attrs = append_attr( $$->attrs, a );
774 | tDEFAULT ':' union_field { attr_t *a = attr_int( @$, ATTR_DEFAULT, 0 );
775 $$ = $3; if (!$$) $$ = make_var( NULL );
776 $$->attrs = append_attr( $$->attrs, a );
780 enums
781 : %empty { $$ = NULL; }
782 | enum_list ',' { $$ = $1; }
783 | enum_list
786 enum_list: enum { if (!$1->eval)
787 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
788 $$ = append_var( NULL, $1 );
790 | enum_list ',' enum { if (!$3->eval)
792 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
793 enum expr_type type = EXPR_NUM;
794 if (last->eval->type == EXPR_HEXNUM) type = EXPR_HEXNUM;
795 if (last->eval->cval + 1 < 0) type = EXPR_HEXNUM;
796 $3->eval = make_exprl(type, last->eval->cval + 1);
798 $$ = append_var( $1, $3 );
802 enum_member: m_attributes ident { $$ = $2;
803 $$->attrs = check_enum_member_attrs($1);
807 enum: enum_member '=' expr_int_const { $$ = reg_const($1);
808 $$->eval = $3;
809 $$->declspec.type = type_new_int(TYPE_BASIC_INT, 0);
811 | enum_member { $$ = reg_const($1);
812 $$->declspec.type = type_new_int(TYPE_BASIC_INT, 0);
816 enumdef: tENUM m_typename '{' enums '}' { $$ = type_new_enum($2, current_namespace, TRUE, $4); }
819 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
820 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
823 m_expr
824 : %empty { $$ = make_expr(EXPR_VOID); }
825 | expr
828 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
829 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
830 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
831 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
832 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
833 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
834 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
835 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
836 | aSQSTRING { $$ = make_exprs(EXPR_CHARCONST, $1); }
837 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
838 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
839 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
840 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
841 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
842 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
843 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
844 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
845 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
846 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
847 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
848 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
849 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
850 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
851 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
852 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
853 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
854 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
855 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
856 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
857 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
858 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
859 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
860 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
861 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
862 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
863 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
864 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
865 | '(' unqualified_decl_spec m_abstract_declarator ')' expr %prec CAST
866 { $$ = make_exprt(EXPR_CAST, declare_var(NULL, $2, $3, 0), $5); free($2); free($3); }
867 | tSIZEOF '(' unqualified_decl_spec m_abstract_declarator ')'
868 { $$ = make_exprt(EXPR_SIZEOF, declare_var(NULL, $3, $4, 0), NULL); free($3); free($4); }
869 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
870 | '(' expr ')' { $$ = $2; }
873 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
874 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
877 expr_int_const: expr { $$ = $1;
878 if (!$$->is_const)
879 error_loc("expression is not an integer constant\n");
883 expr_const: expr { $$ = $1;
884 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
885 error_loc("expression is not constant\n");
889 fields
890 : %empty { $$ = NULL; }
891 | fields field { $$ = append_var_list($1, $2); }
894 field: m_attributes decl_spec struct_declarator_list ';'
895 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
896 check_field_attrs(first, $1);
897 $$ = set_var_types($1, $2, $3);
899 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
900 v->declspec.type = $2; v->attrs = $1;
901 $$ = append_var(NULL, v);
905 ne_union_field:
906 s_field ';' { $$ = $1; }
907 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
910 ne_union_fields
911 : %empty { $$ = NULL; }
912 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
915 union_field:
916 s_field ';' { $$ = $1; }
917 | ';' { $$ = NULL; }
920 s_field: m_attributes decl_spec declarator { $$ = declare_var(check_field_attrs($3->var->name, $1),
921 $2, $3, FALSE);
922 free($3);
924 | m_attributes structdef { var_t *v = make_var(NULL);
925 v->declspec.type = $2; v->attrs = $1;
926 $$ = v;
930 funcdef: declaration { $$ = $1;
931 if (type_get_type($$->declspec.type) != TYPE_FUNCTION)
932 error_loc("only methods may be declared inside the methods section of a dispinterface\n");
933 check_function_attrs($$->name, $$->attrs);
937 declaration:
938 attributes decl_spec init_declarator
939 { $$ = declare_var($1, $2, $3, FALSE);
940 free($3);
942 | decl_spec init_declarator { $$ = declare_var(NULL, $1, $2, FALSE);
943 free($2);
947 m_ident
948 : %empty { $$ = NULL; }
949 | ident
952 m_typename
953 : %empty { $$ = NULL; }
954 | typename
957 typename: aIDENTIFIER
958 | aKNOWNTYPE
961 ident: typename { $$ = make_var($1); }
964 base_type: tBYTE { $$ = find_type_or_error( NULL, "byte" ); }
965 | tWCHAR { $$ = find_type_or_error( NULL, "wchar_t" ); }
966 | int_std
967 | tSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), -1); }
968 | tUNSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), 1); }
969 | tUNSIGNED { $$ = type_new_int(TYPE_BASIC_INT, 1); }
970 | tFLOAT { $$ = find_type_or_error( NULL, "float" ); }
971 | tDOUBLE { $$ = find_type_or_error( NULL, "double" ); }
972 | tBOOLEAN { $$ = find_type_or_error( NULL, "boolean" ); }
973 | tERRORSTATUST { $$ = find_type_or_error( NULL, "error_status_t" ); }
974 | tHANDLET { $$ = find_type_or_error( NULL, "handle_t" ); }
977 m_int
978 : %empty
979 | tINT
982 int_std: tINT { $$ = type_new_int(TYPE_BASIC_INT, 0); }
983 | tSHORT m_int { $$ = type_new_int(TYPE_BASIC_INT16, 0); }
984 | tSMALL { $$ = type_new_int(TYPE_BASIC_INT8, 0); }
985 | tLONG m_int { $$ = type_new_int(TYPE_BASIC_LONG, 0); }
986 | tHYPER m_int { $$ = type_new_int(TYPE_BASIC_HYPER, 0); }
987 | tINT64 { $$ = type_new_int(TYPE_BASIC_INT64, 0); }
988 | tCHAR { $$ = type_new_int(TYPE_BASIC_CHAR, 0); }
989 | tINT32 { $$ = type_new_int(TYPE_BASIC_INT32, 0); }
990 | tINT3264 { $$ = type_new_int(TYPE_BASIC_INT3264, 0); }
993 namespace_pfx:
994 aIDENTIFIER '.' { $$ = find_namespace_or_error(&global_namespace, $1); }
995 | namespace_pfx aIDENTIFIER '.' { $$ = find_namespace_or_error($1, $2); }
998 qualified_type:
999 typename { $$ = find_type_or_error(current_namespace, $1); }
1000 | namespace_pfx typename { $$ = find_type_or_error($1, $2); }
1003 parameterized_type: qualified_type '<' parameterized_type_args '>'
1004 { $$ = find_parameterized_type($1, $3); }
1007 parameterized_type_arg:
1008 base_type { $$ = $1; }
1009 | qualified_type { $$ = $1; }
1010 | qualified_type '*' { $$ = type_new_pointer($1); }
1011 | parameterized_type { $$ = $1; }
1012 | parameterized_type '*' { $$ = type_new_pointer($1); }
1015 parameterized_type_args:
1016 parameterized_type_arg { $$ = append_typeref(NULL, make_typeref($1)); }
1017 | parameterized_type_args ',' parameterized_type_arg
1018 { $$ = append_typeref($1, make_typeref($3)); }
1021 coclass: tCOCLASS typename { $$ = type_coclass_declare($2); }
1024 coclassdef: attributes coclass '{' class_interfaces '}' semicolon_opt
1025 { $$ = type_coclass_define($2, $1, $4); }
1028 runtimeclass: tRUNTIMECLASS typename { $$ = type_runtimeclass_declare($2, current_namespace); }
1031 runtimeclass_def: attributes runtimeclass inherit '{' class_interfaces '}' semicolon_opt
1032 { if ($3 && type_get_type($3) != TYPE_RUNTIMECLASS) error_loc("%s is not a runtimeclass\n", $3->name);
1033 $$ = type_runtimeclass_define($2, $1, $5); }
1036 apicontract: tAPICONTRACT typename { $$ = type_apicontract_declare($2, current_namespace); }
1039 apicontract_def: attributes apicontract '{' '}' semicolon_opt
1040 { $$ = type_apicontract_define($2, $1); }
1043 namespacedef: tNAMESPACE aIDENTIFIER { $$ = append_str( NULL, $2 ); }
1044 | namespacedef '.' aIDENTIFIER { $$ = append_str( $1, $3 ); }
1047 class_interfaces
1048 : %empty { $$ = NULL; }
1049 | class_interfaces class_interface { $$ = append_typeref( $1, $2 ); }
1052 class_interface:
1053 m_attributes interfaceref ';' { $$ = make_typeref($2); $$->attrs = $1; }
1054 | m_attributes dispinterfaceref ';' { $$ = make_typeref($2); $$->attrs = $1; }
1057 dispinterface: tDISPINTERFACE typename { $$ = type_dispinterface_declare($2); }
1060 dispattributes: attributes { $$ = append_attr( $1, attr_int( @$, ATTR_DISPINTERFACE, 0 ) ); }
1063 dispint_props: tPROPERTIES ':' { $$ = NULL; }
1064 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
1067 dispint_meths: tMETHODS ':' { $$ = NULL; }
1068 | dispint_meths funcdef ';' { $$ = append_var( $1, $2 ); }
1071 dispinterfacedef:
1072 dispattributes dispinterface '{' dispint_props dispint_meths '}'
1073 { $$ = type_dispinterface_define($2, $1, $4, $5); }
1074 | dispattributes dispinterface '{' interface ';' '}'
1075 { $$ = type_dispinterface_define_from_iface($2, $1, $4); }
1078 inherit
1079 : %empty { $$ = NULL; }
1080 | ':' qualified_type { $$ = $2; }
1081 | ':' parameterized_type { $$ = $2; }
1084 type_parameter: typename { $$ = get_type(TYPE_PARAMETER, $1, parameters_namespace, 0); }
1087 type_parameters:
1088 type_parameter { $$ = append_typeref(NULL, make_typeref($1)); }
1089 | type_parameters ',' type_parameter { $$ = append_typeref($1, make_typeref($3)); }
1092 interface:
1093 tINTERFACE typename { $$ = type_interface_declare($2, current_namespace); }
1094 | tINTERFACE typename '<' { push_parameters_namespace($2); } type_parameters { pop_parameters_namespace($2); } '>'
1095 { $$ = type_parameterized_interface_declare($2, current_namespace, $5); }
1098 delegatedef: m_attributes tDELEGATE type ident '(' m_args ')' semicolon_opt
1099 { $$ = type_delegate_declare($4->name, current_namespace);
1100 $$ = type_delegate_define($$, $1, append_statement(NULL, make_statement_delegate($3, $6)));
1102 | m_attributes tDELEGATE type ident
1103 '<' { push_parameters_namespace($4->name); } type_parameters '>'
1104 '(' m_args ')' { pop_parameters_namespace($4->name); } semicolon_opt
1105 { $$ = type_parameterized_delegate_declare($4->name, current_namespace, $7);
1106 $$ = type_parameterized_delegate_define($$, $1, append_statement(NULL, make_statement_delegate($3, $10)));
1110 required_types:
1111 qualified_type { $$ = append_typeref(NULL, make_typeref($1)); }
1112 | parameterized_type { $$ = append_typeref(NULL, make_typeref($1)); }
1113 | required_types ',' qualified_type { $$ = append_typeref($1, make_typeref($3)); }
1114 | required_types ',' parameterized_type { $$ = append_typeref($1, make_typeref($3)); }
1117 requires
1118 : %empty { $$ = NULL; }
1119 | tREQUIRES required_types { $$ = $2; }
1122 interfacedef: attributes interface { if ($2->type_type == TYPE_PARAMETERIZED_TYPE) push_parameters_namespace($2->name); }
1123 inherit requires '{' int_statements '}' semicolon_opt
1124 { if ($2->type_type == TYPE_PARAMETERIZED_TYPE)
1126 $$ = type_parameterized_interface_define($2, $1, $4, $7, $5);
1127 pop_parameters_namespace($2->name);
1129 else
1131 $$ = type_interface_define($2, $1, $4, $7, $5);
1132 check_async_uuid($$);
1135 | dispinterfacedef semicolon_opt { $$ = $1; }
1138 interfaceref:
1139 tINTERFACE typename { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
1140 | tINTERFACE namespace_pfx typename { $$ = get_type(TYPE_INTERFACE, $3, $2, 0); }
1141 | tINTERFACE parameterized_type { if (type_get_type(($$ = $2)) != TYPE_INTERFACE) error_loc("%s is not an interface\n", $$->name); }
1144 dispinterfaceref:
1145 tDISPINTERFACE typename { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
1148 module: tMODULE typename { $$ = type_module_declare($2); }
1151 moduledef: m_attributes module '{' int_statements '}' semicolon_opt
1152 { $$ = type_module_define($2, $1, $4); }
1155 storage_cls_spec:
1156 tEXTERN { $$ = STG_EXTERN; }
1157 | tSTATIC { $$ = STG_STATIC; }
1158 | tREGISTER { $$ = STG_REGISTER; }
1161 function_specifier:
1162 tINLINE { $$ = FUNCTION_SPECIFIER_INLINE; }
1165 type_qualifier:
1166 tCONST { $$ = TYPE_QUALIFIER_CONST; }
1169 m_type_qual_list
1170 : %empty { $$ = 0; }
1171 | m_type_qual_list type_qualifier { $$ = $1 | $2; }
1174 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, STG_NONE, 0, 0); }
1175 | decl_spec_no_type type m_decl_spec_no_type
1176 { $$ = make_decl_spec($2, $1, $3, STG_NONE, 0, 0); }
1179 unqualified_decl_spec: unqualified_type m_decl_spec_no_type
1180 { $$ = make_decl_spec($1, $2, NULL, STG_NONE, 0, 0); }
1181 | decl_spec_no_type unqualified_type m_decl_spec_no_type
1182 { $$ = make_decl_spec($2, $1, $3, STG_NONE, 0, 0); }
1185 m_decl_spec_no_type
1186 : %empty { $$ = NULL; }
1187 | decl_spec_no_type
1190 decl_spec_no_type:
1191 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, STG_NONE, $1, 0); }
1192 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, STG_NONE, 0, $1); }
1193 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, 0, 0); }
1196 declarator:
1197 '*' m_type_qual_list declarator %prec PPTR
1198 { $$ = $3; append_chain_type($$, type_new_pointer(NULL), $2); }
1199 | callconv declarator { $$ = $2; append_chain_callconv( @$, $$->type, $1 ); }
1200 | direct_declarator
1203 direct_declarator:
1204 ident { $$ = make_declarator($1); }
1205 | '(' declarator ')' { $$ = $2; }
1206 | direct_declarator array { $$ = $1; append_array($$, $2); }
1207 | direct_declarator '(' m_args ')' { $$ = $1; append_chain_type($$, type_new_function($3), 0); }
1210 /* abstract declarator */
1211 abstract_declarator:
1212 '*' m_type_qual_list m_abstract_declarator %prec PPTR
1213 { $$ = $3; append_chain_type($$, type_new_pointer(NULL), $2); }
1214 | callconv m_abstract_declarator { $$ = $2; append_chain_callconv( @$, $$->type, $1 ); }
1215 | abstract_direct_declarator
1218 /* abstract declarator without accepting direct declarator */
1219 abstract_declarator_no_direct:
1220 '*' m_type_qual_list m_any_declarator %prec PPTR
1221 { $$ = $3; append_chain_type($$, type_new_pointer(NULL), $2); }
1222 | callconv m_any_declarator { $$ = $2; append_chain_callconv( @$, $$->type, $1 ); }
1225 /* abstract declarator or empty */
1226 m_abstract_declarator
1227 : %empty { $$ = make_declarator(NULL); }
1228 | abstract_declarator
1231 /* abstract direct declarator */
1232 abstract_direct_declarator:
1233 '(' abstract_declarator_no_direct ')' { $$ = $2; }
1234 | abstract_direct_declarator array { $$ = $1; append_array($$, $2); }
1235 | array { $$ = make_declarator(NULL); append_array($$, $1); }
1236 | '(' m_args ')'
1237 { $$ = make_declarator(NULL);
1238 append_chain_type($$, type_new_function($2), 0);
1240 | abstract_direct_declarator '(' m_args ')'
1241 { $$ = $1;
1242 append_chain_type($$, type_new_function($3), 0);
1246 /* abstract or non-abstract declarator */
1247 any_declarator:
1248 '*' m_type_qual_list m_any_declarator %prec PPTR
1249 { $$ = $3; append_chain_type($$, type_new_pointer(NULL), $2); }
1250 | callconv m_any_declarator { $$ = $2; append_chain_callconv( @$, $$->type, $1 ); }
1251 | any_direct_declarator
1254 /* abstract or non-abstract declarator without accepting direct declarator */
1255 any_declarator_no_direct:
1256 '*' m_type_qual_list m_any_declarator %prec PPTR
1257 { $$ = $3; append_chain_type($$, type_new_pointer(NULL), $2); }
1258 | callconv m_any_declarator { $$ = $2; append_chain_callconv( @$, $$->type, $1 ); }
1261 /* abstract or non-abstract declarator or empty */
1262 m_any_declarator
1263 : %empty { $$ = make_declarator(NULL); }
1264 | any_declarator
1267 /* abstract or non-abstract direct declarator. note: direct declarators
1268 * aren't accepted inside brackets to avoid ambiguity with the rule for
1269 * function arguments */
1270 any_direct_declarator:
1271 ident { $$ = make_declarator($1); }
1272 | '(' any_declarator_no_direct ')' { $$ = $2; }
1273 | any_direct_declarator array { $$ = $1; append_array($$, $2); }
1274 | array { $$ = make_declarator(NULL); append_array($$, $1); }
1275 | '(' m_args ')'
1276 { $$ = make_declarator(NULL);
1277 append_chain_type($$, type_new_function($2), 0);
1279 | any_direct_declarator '(' m_args ')'
1280 { $$ = $1;
1281 append_chain_type($$, type_new_function($3), 0);
1285 declarator_list:
1286 declarator { $$ = append_declarator( NULL, $1 ); }
1287 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
1290 m_bitfield
1291 : %empty { $$ = NULL; }
1292 | ':' expr_const { $$ = $2; }
1295 struct_declarator: any_declarator m_bitfield { $$ = $1; $$->bits = $2;
1296 if (!$$->bits && !$$->var->name)
1297 error_loc("unnamed fields are not allowed\n");
1301 struct_declarator_list:
1302 struct_declarator { $$ = append_declarator( NULL, $1 ); }
1303 | struct_declarator_list ',' struct_declarator
1304 { $$ = append_declarator( $1, $3 ); }
1307 init_declarator:
1308 declarator { $$ = $1; }
1309 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
1312 threading_type:
1313 tAPARTMENT { $$ = THREADING_APARTMENT; }
1314 | tNEUTRAL { $$ = THREADING_NEUTRAL; }
1315 | tSINGLE { $$ = THREADING_SINGLE; }
1316 | tFREE { $$ = THREADING_FREE; }
1317 | tBOTH { $$ = THREADING_BOTH; }
1318 | tMTA { $$ = THREADING_FREE; }
1321 pointer_type:
1322 tREF { $$ = FC_RP; }
1323 | tUNIQUE { $$ = FC_UP; }
1324 | tPTR { $$ = FC_FP; }
1327 structdef: tSTRUCT m_typename '{' fields '}' { $$ = type_new_struct($2, current_namespace, TRUE, $4); }
1330 unqualified_type:
1331 tVOID { $$ = type_new_void(); }
1332 | base_type { $$ = $1; }
1333 | enumdef { $$ = $1; }
1334 | tENUM aIDENTIFIER { $$ = type_new_enum($2, current_namespace, FALSE, NULL); }
1335 | structdef { $$ = $1; }
1336 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, current_namespace, FALSE, NULL); }
1337 | uniondef { $$ = $1; }
1338 | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, current_namespace, FALSE, NULL); }
1339 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
1340 | aKNOWNTYPE { $$ = find_type_or_error(current_namespace, $1); }
1343 type:
1344 unqualified_type
1345 | namespace_pfx typename { $$ = find_type_or_error($1, $2); }
1346 | parameterized_type { $$ = $1; }
1349 typedef: m_attributes tTYPEDEF m_attributes decl_spec declarator_list
1350 { $1 = append_attribs($1, $3);
1351 reg_typedefs( @$, $4, $5, check_typedef_attrs( $1 ) );
1352 $$ = make_statement_typedef($5, !$4->type->defined);
1356 uniondef: tUNION m_typename '{' ne_union_fields '}'
1357 { $$ = type_new_nonencapsulated_union($2, current_namespace, TRUE, $4); }
1358 | tUNION m_typename
1359 tSWITCH '(' s_field ')'
1360 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1363 version:
1364 aNUM { $$ = MAKEVERSION($1, 0); }
1365 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1366 | aHEXNUM { $$ = $1; }
1369 acf_statements
1370 : %empty
1371 | acf_interface acf_statements
1374 acf_int_statements
1375 : %empty
1376 | acf_int_statement acf_int_statements
1379 acf_int_statement
1380 : tTYPEDEF acf_attributes aKNOWNTYPE ';'
1381 { type_t *type = find_type_or_error(current_namespace, $3);
1382 type->attrs = append_attr_list(type->attrs, $2);
1386 acf_interface
1387 : acf_attributes tINTERFACE aKNOWNTYPE '{' acf_int_statements '}'
1388 { type_t *iface = find_type_or_error(current_namespace, $3);
1389 if (type_get_type(iface) != TYPE_INTERFACE)
1390 error_loc("%s is not an interface\n", iface->name);
1391 iface->attrs = append_attr_list(iface->attrs, $1);
1395 acf_attributes
1396 : %empty { $$ = NULL; }
1397 | '[' acf_attribute_list ']' { $$ = $2; }
1400 acf_attribute_list
1401 : acf_attribute { $$ = append_attr(NULL, $1); }
1402 | acf_attribute_list ',' acf_attribute { $$ = append_attr($1, $3); }
1405 acf_attribute
1406 : tALLOCATE '(' allocate_option_list ')'
1407 { $$ = attr_int( @$, ATTR_ALLOCATE, $3 ); }
1408 | tENCODE { $$ = attr_int( @$, ATTR_ENCODE, 0 ); }
1409 | tDECODE { $$ = attr_int( @$, ATTR_DECODE, 0 ); }
1410 | tEXPLICITHANDLE { $$ = attr_int( @$, ATTR_EXPLICIT_HANDLE, 0 ); }
1413 allocate_option_list
1414 : allocate_option { $$ = $1; }
1415 | allocate_option_list ',' allocate_option
1416 { $$ = $1 | $3; }
1419 allocate_option
1420 : tDONTFREE { $$ = FC_DONT_FREE; }
1421 | tFREE { $$ = 0; }
1422 | tALLNODES { $$ = FC_ALLOCATE_ALL_NODES; }
1423 | tSINGLENODE { $$ = 0; }
1428 static void decl_builtin_basic(const char *name, enum type_basic_type type)
1430 type_t *t = type_new_basic(type);
1431 reg_type(t, name, NULL, 0);
1434 static void decl_builtin_alias(const char *name, type_t *t)
1436 const decl_spec_t ds = {.type = t};
1437 reg_type(type_new_alias(&ds, name), name, NULL, 0);
1440 void init_types(void)
1442 decl_builtin_basic("byte", TYPE_BASIC_BYTE);
1443 decl_builtin_basic("wchar_t", TYPE_BASIC_WCHAR);
1444 decl_builtin_basic("float", TYPE_BASIC_FLOAT);
1445 decl_builtin_basic("double", TYPE_BASIC_DOUBLE);
1446 decl_builtin_basic("error_status_t", TYPE_BASIC_ERROR_STATUS_T);
1447 decl_builtin_basic("handle_t", TYPE_BASIC_HANDLE);
1448 decl_builtin_alias("boolean", type_new_basic(TYPE_BASIC_CHAR));
1451 static str_list_t *append_str(str_list_t *list, char *str)
1453 struct str_list_entry_t *entry;
1455 if (!str) return list;
1456 if (!list)
1458 list = xmalloc( sizeof(*list) );
1459 list_init( list );
1461 entry = xmalloc( sizeof(*entry) );
1462 entry->str = str;
1463 list_add_tail( list, &entry->entry );
1464 return list;
1468 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right,
1469 enum storage_class stgclass, enum type_qualifier qual, enum function_specifier func_specifier)
1471 decl_spec_t *declspec = left ? left : right;
1472 if (!declspec)
1474 declspec = xmalloc(sizeof(*declspec));
1475 declspec->type = NULL;
1476 declspec->stgclass = STG_NONE;
1477 declspec->qualifier = 0;
1478 declspec->func_specifier = 0;
1480 declspec->type = type;
1481 if (left && declspec != left)
1483 if (declspec->stgclass == STG_NONE)
1484 declspec->stgclass = left->stgclass;
1485 else if (left->stgclass != STG_NONE)
1486 error_loc("only one storage class can be specified\n");
1487 declspec->qualifier |= left->qualifier;
1488 declspec->func_specifier |= left->func_specifier;
1489 assert(!left->type);
1490 free(left);
1492 if (right && declspec != right)
1494 if (declspec->stgclass == STG_NONE)
1495 declspec->stgclass = right->stgclass;
1496 else if (right->stgclass != STG_NONE)
1497 error_loc("only one storage class can be specified\n");
1498 declspec->qualifier |= right->qualifier;
1499 declspec->func_specifier |= right->func_specifier;
1500 assert(!right->type);
1501 free(right);
1504 if (declspec->stgclass == STG_NONE)
1505 declspec->stgclass = stgclass;
1506 else if (stgclass != STG_NONE)
1507 error_loc("only one storage class can be specified\n");
1508 declspec->qualifier |= qual;
1509 declspec->func_specifier |= func_specifier;
1511 return declspec;
1514 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1516 if (!expr) return list;
1517 if (!list)
1519 list = xmalloc( sizeof(*list) );
1520 list_init( list );
1522 list_add_tail( list, &expr->entry );
1523 return list;
1526 static void append_array(declarator_t *decl, expr_t *expr)
1528 type_t *array;
1530 if (!expr)
1531 return;
1533 /* An array is always a reference pointer unless explicitly marked otherwise
1534 * (regardless of what the default pointer attribute is). */
1535 array = type_new_array(NULL, NULL, FALSE, expr->is_const ? expr->cval : 0,
1536 expr->is_const ? NULL : expr, NULL);
1538 append_chain_type(decl, array, 0);
1541 static struct list type_pool = LIST_INIT(type_pool);
1542 typedef struct
1544 type_t data;
1545 struct list link;
1546 } type_pool_node_t;
1548 type_t *alloc_type(void)
1550 type_pool_node_t *node = xmalloc(sizeof *node);
1551 list_add_tail(&type_pool, &node->link);
1552 return &node->data;
1555 void set_all_tfswrite(int val)
1557 type_pool_node_t *node;
1558 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1559 node->data.tfswrite = val;
1562 void clear_all_offsets(void)
1564 type_pool_node_t *node;
1565 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1566 node->data.typestring_offset = node->data.ptrdesc = 0;
1569 static void type_function_add_head_arg(type_t *type, var_t *arg)
1571 if (!type->details.function->args)
1573 type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
1574 list_init( type->details.function->args );
1576 list_add_head( type->details.function->args, &arg->entry );
1579 static int is_allowed_range_type(const type_t *type)
1581 switch (type_get_type(type))
1583 case TYPE_ENUM:
1584 return TRUE;
1585 case TYPE_BASIC:
1586 switch (type_basic_get_type(type))
1588 case TYPE_BASIC_INT8:
1589 case TYPE_BASIC_INT16:
1590 case TYPE_BASIC_INT32:
1591 case TYPE_BASIC_INT64:
1592 case TYPE_BASIC_INT:
1593 case TYPE_BASIC_INT3264:
1594 case TYPE_BASIC_LONG:
1595 case TYPE_BASIC_BYTE:
1596 case TYPE_BASIC_CHAR:
1597 case TYPE_BASIC_WCHAR:
1598 case TYPE_BASIC_HYPER:
1599 return TRUE;
1600 case TYPE_BASIC_FLOAT:
1601 case TYPE_BASIC_DOUBLE:
1602 case TYPE_BASIC_ERROR_STATUS_T:
1603 case TYPE_BASIC_HANDLE:
1604 return FALSE;
1606 return FALSE;
1607 default:
1608 return FALSE;
1612 static type_t *get_chain_ref(type_t *type)
1614 if (is_ptr(type))
1615 return type_pointer_get_ref_type(type);
1616 else if (is_array(type))
1617 return type_array_get_element_type(type);
1618 else if (is_func(type))
1619 return type_function_get_rettype(type);
1620 return NULL;
1623 static type_t *get_chain_end(type_t *type)
1625 type_t *inner;
1626 while ((inner = get_chain_ref(type)))
1627 type = inner;
1628 return type;
1631 static void append_chain_type(declarator_t *decl, type_t *type, enum type_qualifier qual)
1633 type_t *chain_type;
1635 if (!decl->type)
1637 decl->type = type;
1638 decl->qualifier = qual;
1639 return;
1641 chain_type = get_chain_end(decl->type);
1643 if (is_ptr(chain_type))
1645 chain_type->details.pointer.ref.type = type;
1646 chain_type->details.pointer.ref.qualifier = qual;
1648 else if (is_array(chain_type))
1650 chain_type->details.array.elem.type = type;
1651 chain_type->details.array.elem.qualifier = qual;
1653 else if (is_func(chain_type))
1655 chain_type->details.function->retval->declspec.type = type;
1656 chain_type->details.function->retval->declspec.qualifier = qual;
1658 else
1659 assert(0);
1661 if (!is_func(chain_type))
1662 type->attrs = move_attr(type->attrs, chain_type->attrs, ATTR_CALLCONV);
1665 static void append_chain_callconv( struct location where, type_t *chain, char *callconv )
1667 type_t *chain_end;
1669 if (chain && (chain_end = get_chain_end(chain)))
1670 chain_end->attrs = append_attr( chain_end->attrs, attr_ptr( where, ATTR_CALLCONV, callconv ) );
1671 else
1672 error_loc("calling convention applied to non-function type\n");
1675 static warning_list_t *append_warning(warning_list_t *list, int num)
1677 warning_t *entry;
1679 if(!list)
1681 list = xmalloc( sizeof(*list) );
1682 list_init( list );
1684 entry = xmalloc( sizeof(*entry) );
1685 entry->num = num;
1686 list_add_tail( list, &entry->entry );
1687 return list;
1690 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_t *decl,
1691 int top)
1693 var_t *v = decl->var;
1694 expr_list_t *sizes = get_attrp(attrs, ATTR_SIZEIS);
1695 expr_list_t *lengs = get_attrp(attrs, ATTR_LENGTHIS);
1696 expr_t *dim;
1697 type_t **ptype;
1698 type_t *type = decl_spec->type;
1700 if (decl_spec->func_specifier & FUNCTION_SPECIFIER_INLINE)
1702 if (!decl || !is_func(decl->type))
1703 error_loc("inline attribute applied to non-function type\n");
1706 /* add type onto the end of the pointers in pident->type */
1707 append_chain_type(decl, type, decl_spec->qualifier);
1708 v->declspec = *decl_spec;
1709 v->declspec.type = decl->type;
1710 v->declspec.qualifier = decl->qualifier;
1711 v->attrs = attrs;
1712 v->declonly = !type->defined;
1714 if (is_attr(type->attrs, ATTR_CALLCONV) && !is_func(type))
1715 error_loc("calling convention applied to non-function type\n");
1717 /* check for pointer attribute being applied to non-pointer, non-array
1718 * type */
1719 if (!is_array(v->declspec.type))
1721 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1722 const type_t *ptr = NULL;
1723 for (ptr = v->declspec.type; ptr && !ptr_attr; )
1725 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1726 if (!ptr_attr && type_is_alias(ptr))
1727 ptr = type_alias_get_aliasee_type(ptr);
1728 else
1729 break;
1731 if (is_ptr(ptr))
1733 if (ptr_attr && ptr_attr != FC_UP &&
1734 type_get_type(type_pointer_get_ref_type(ptr)) == TYPE_INTERFACE)
1735 warning_at( &v->where, "%s: pointer attribute applied to interface pointer type has no effect\n", v->name );
1736 if (!ptr_attr && top)
1738 /* FIXME: this is a horrible hack to cope with the issue that we
1739 * store an offset to the typeformat string in the type object, but
1740 * two typeformat strings may be written depending on whether the
1741 * pointer is a toplevel parameter or not */
1742 v->declspec.type = duptype(v->declspec.type, 1);
1745 else if (ptr_attr)
1746 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1749 if (is_attr(v->attrs, ATTR_STRING))
1751 type_t *t = type;
1753 if (!is_ptr(v->declspec.type) && !is_array(v->declspec.type))
1754 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1755 v->name);
1757 for (;;)
1759 if (is_ptr(t))
1760 t = type_pointer_get_ref_type(t);
1761 else if (is_array(t))
1762 t = type_array_get_element_type(t);
1763 else
1764 break;
1767 if (type_get_type(t) != TYPE_BASIC &&
1768 (get_basic_fc(t) != FC_CHAR &&
1769 get_basic_fc(t) != FC_BYTE &&
1770 get_basic_fc(t) != FC_WCHAR))
1772 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1773 v->name);
1777 if (is_attr(v->attrs, ATTR_V1ENUM))
1779 if (type_get_type_detect_alias(v->declspec.type) != TYPE_ENUM)
1780 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1783 if (is_attr(v->attrs, ATTR_RANGE) && !is_allowed_range_type(v->declspec.type))
1784 error_loc("'%s': [range] attribute applied to non-integer type\n",
1785 v->name);
1787 ptype = &v->declspec.type;
1788 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1790 if (dim->type != EXPR_VOID)
1792 if (is_array(*ptype))
1794 if (!type_array_get_conformance(*ptype) ||
1795 type_array_get_conformance(*ptype)->type != EXPR_VOID)
1796 error_loc("%s: cannot specify size_is for an already sized array\n", v->name);
1797 else
1798 *ptype = type_new_array((*ptype)->name,
1799 type_array_get_element(*ptype), FALSE,
1800 0, dim, NULL);
1802 else if (is_ptr(*ptype))
1803 *ptype = type_new_array((*ptype)->name, type_pointer_get_ref(*ptype), TRUE,
1804 0, dim, NULL);
1805 else
1806 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1809 if (is_ptr(*ptype))
1810 ptype = &(*ptype)->details.pointer.ref.type;
1811 else if (is_array(*ptype))
1812 ptype = &(*ptype)->details.array.elem.type;
1813 else
1814 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1817 ptype = &v->declspec.type;
1818 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1820 if (dim->type != EXPR_VOID)
1822 if (is_array(*ptype))
1824 *ptype = type_new_array((*ptype)->name,
1825 type_array_get_element(*ptype),
1826 type_array_is_decl_as_ptr(*ptype),
1827 type_array_get_dim(*ptype),
1828 type_array_get_conformance(*ptype), dim);
1830 else
1831 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1834 if (is_ptr(*ptype))
1835 ptype = &(*ptype)->details.pointer.ref.type;
1836 else if (is_array(*ptype))
1837 ptype = &(*ptype)->details.array.elem.type;
1838 else
1839 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1842 if (decl->bits)
1843 v->declspec.type = type_new_bitfield(v->declspec.type, decl->bits);
1845 return v;
1848 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1850 declarator_t *decl, *next;
1851 var_list_t *var_list = NULL;
1853 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1855 var_t *var = declare_var(attrs, decl_spec, decl, 0);
1856 var_list = append_var(var_list, var);
1857 free(decl);
1859 free(decl_spec);
1860 return var_list;
1863 typeref_list_t *append_typeref(typeref_list_t *list, typeref_t *ref)
1865 if (!ref) return list;
1866 if (!list)
1868 list = xmalloc( sizeof(*list) );
1869 list_init( list );
1871 list_add_tail( list, &ref->entry );
1872 return list;
1875 typeref_t *make_typeref(type_t *type)
1877 typeref_t *ref = xmalloc(sizeof(typeref_t));
1878 ref->type = type;
1879 ref->attrs = NULL;
1880 return ref;
1883 var_list_t *append_var(var_list_t *list, var_t *var)
1885 if (!var) return list;
1886 if (!list)
1888 list = xmalloc( sizeof(*list) );
1889 list_init( list );
1891 list_add_tail( list, &var->entry );
1892 return list;
1895 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1897 if (!vars) return list;
1898 if (!list)
1900 list = xmalloc( sizeof(*list) );
1901 list_init( list );
1903 list_move_tail( list, vars );
1904 return list;
1907 var_t *make_var(char *name)
1909 var_t *v = xmalloc(sizeof(var_t));
1910 v->name = name;
1911 init_declspec(&v->declspec, NULL);
1912 v->attrs = NULL;
1913 v->eval = NULL;
1914 init_location( &v->where, NULL, NULL );
1915 v->declonly = FALSE;
1916 return v;
1919 static var_t *copy_var(var_t *src, char *name, map_attrs_filter_t attr_filter)
1921 var_t *v = xmalloc(sizeof(var_t));
1922 v->name = name;
1923 v->declspec = src->declspec;
1924 v->attrs = map_attrs(src->attrs, attr_filter);
1925 v->eval = src->eval;
1926 v->where = src->where;
1927 return v;
1930 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1932 if (!d) return list;
1933 if (!list) {
1934 list = xmalloc(sizeof(*list));
1935 list_init(list);
1937 list_add_tail(list, &d->entry);
1938 return list;
1941 static declarator_t *make_declarator(var_t *var)
1943 declarator_t *d = xmalloc(sizeof(*d));
1944 d->var = var ? var : make_var(NULL);
1945 d->type = NULL;
1946 d->qualifier = 0;
1947 d->bits = NULL;
1948 return d;
1951 static type_t *make_safearray(type_t *type)
1953 decl_spec_t ds = {.type = type};
1954 ds.type = type_new_alias(&ds, "SAFEARRAY");
1955 return type_new_array(NULL, &ds, TRUE, 0, NULL, NULL);
1958 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1960 typelib_t *typelib = xmalloc(sizeof(*typelib));
1961 memset(typelib, 0, sizeof(*typelib));
1962 typelib->name = xstrdup(name);
1963 typelib->attrs = attrs;
1964 list_init( &typelib->importlibs );
1965 return typelib;
1968 static int hash_ident(const char *name)
1970 const char *p = name;
1971 int sum = 0;
1972 /* a simple sum hash is probably good enough */
1973 while (*p) {
1974 sum += *p;
1975 p++;
1977 return sum & (HASHMAX-1);
1980 /***** type repository *****/
1982 static struct namespace *find_sub_namespace(struct namespace *namespace, const char *name)
1984 struct namespace *cur;
1986 LIST_FOR_EACH_ENTRY(cur, &namespace->children, struct namespace, entry) {
1987 if(!strcmp(cur->name, name))
1988 return cur;
1991 return NULL;
1994 static void push_namespace(const char *name)
1996 struct namespace *namespace;
1998 namespace = find_sub_namespace(current_namespace, name);
1999 if(!namespace) {
2000 namespace = xmalloc(sizeof(*namespace));
2001 namespace->name = xstrdup(name);
2002 namespace->parent = current_namespace;
2003 list_add_tail(&current_namespace->children, &namespace->entry);
2004 list_init(&namespace->children);
2005 memset(namespace->type_hash, 0, sizeof(namespace->type_hash));
2008 current_namespace = namespace;
2011 static void pop_namespace(const char *name)
2013 assert(!strcmp(current_namespace->name, name) && current_namespace->parent);
2014 current_namespace = current_namespace->parent;
2017 static void push_namespaces(str_list_t *names)
2019 const struct str_list_entry_t *name;
2020 LIST_FOR_EACH_ENTRY(name, names, const struct str_list_entry_t, entry)
2021 push_namespace(name->str);
2024 static void pop_namespaces(str_list_t *names)
2026 const struct str_list_entry_t *name;
2027 LIST_FOR_EACH_ENTRY_REV(name, names, const struct str_list_entry_t, entry)
2028 pop_namespace(name->str);
2031 static void push_parameters_namespace(const char *name)
2033 struct namespace *namespace;
2035 if (!(namespace = find_sub_namespace(current_namespace, name)))
2037 namespace = xmalloc(sizeof(*namespace));
2038 namespace->name = xstrdup(name);
2039 namespace->parent = current_namespace;
2040 list_add_tail(&current_namespace->children, &namespace->entry);
2041 list_init(&namespace->children);
2042 memset(namespace->type_hash, 0, sizeof(namespace->type_hash));
2045 parameters_namespace = namespace;
2048 static void pop_parameters_namespace(const char *name)
2050 assert(!strcmp(parameters_namespace->name, name) && parameters_namespace->parent);
2051 parameters_namespace = NULL;
2054 struct rtype {
2055 const char *name;
2056 type_t *type;
2057 int t;
2058 struct rtype *next;
2061 type_t *reg_type(type_t *type, const char *name, struct namespace *namespace, int t)
2063 struct rtype *nt;
2064 int hash;
2065 if (!name) {
2066 error_loc("registering named type without name\n");
2067 return type;
2069 if (!namespace)
2070 namespace = &global_namespace;
2071 hash = hash_ident(name);
2072 nt = xmalloc(sizeof(struct rtype));
2073 nt->name = name;
2074 if (is_global_namespace(namespace))
2076 type->c_name = name;
2077 type->qualified_name = name;
2079 else
2081 type->c_name = format_namespace(namespace, "__x_", "_C", name, use_abi_namespace ? "ABI" : NULL);
2082 type->qualified_name = format_namespace(namespace, "", "::", name, use_abi_namespace ? "ABI" : NULL);
2084 nt->type = type;
2085 nt->t = t;
2086 nt->next = namespace->type_hash[hash];
2087 namespace->type_hash[hash] = nt;
2088 return type;
2091 static type_t *reg_typedefs( struct location where, decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs )
2093 declarator_t *decl;
2094 type_t *type = decl_spec->type;
2096 if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
2097 attrs = append_attr( attrs, attr_int( where, ATTR_PUBLIC, 0 ) );
2099 /* We must generate names for tagless enum, struct or union.
2100 Typedef-ing a tagless enum, struct or union means we want the typedef
2101 to be included in a library hence the public attribute. */
2102 if (type_get_type_detect_alias(type) == TYPE_ENUM ||
2103 type_get_type_detect_alias(type) == TYPE_STRUCT ||
2104 type_get_type_detect_alias(type) == TYPE_UNION ||
2105 type_get_type_detect_alias(type) == TYPE_ENCAPSULATED_UNION)
2107 if (!type->name)
2109 type->name = gen_name();
2110 if (!is_attr(attrs, ATTR_PUBLIC))
2111 attrs = append_attr( attrs, attr_int( where, ATTR_PUBLIC, 0 ) );
2114 /* replace existing attributes when generating a typelib */
2115 if (do_typelib)
2116 type->attrs = attrs;
2119 LIST_FOR_EACH_ENTRY( decl, decls, declarator_t, entry )
2122 if (decl->var->name) {
2123 type_t *cur;
2124 var_t *name;
2126 cur = find_type(decl->var->name, current_namespace, 0);
2129 * MIDL allows shadowing types that are declared in imported files.
2130 * We don't throw an error in this case and instead add a new type
2131 * (which is earlier on the list in hash table, so it will be used
2132 * instead of shadowed type).
2134 * FIXME: We may consider string separated type tables for each input
2135 * for cleaner solution.
2137 if (cur && input_name == cur->where.input_name)
2138 error_loc( "%s: redefinition error; original definition was at %s:%d\n",
2139 cur->name, cur->where.input_name, cur->where.first_line );
2141 name = declare_var(attrs, decl_spec, decl, 0);
2142 cur = type_new_alias(&name->declspec, name->name);
2143 cur->attrs = attrs;
2145 reg_type(cur, cur->name, current_namespace, 0);
2148 return type;
2151 type_t *find_type(const char *name, struct namespace *namespace, int t)
2153 struct rtype *cur;
2155 if(namespace && namespace != &global_namespace) {
2156 for(cur = namespace->type_hash[hash_ident(name)]; cur; cur = cur->next) {
2157 if(cur->t == t && !strcmp(cur->name, name))
2158 return cur->type;
2161 for(cur = global_namespace.type_hash[hash_ident(name)]; cur; cur = cur->next) {
2162 if(cur->t == t && !strcmp(cur->name, name))
2163 return cur->type;
2165 return NULL;
2168 static type_t *find_type_or_error(struct namespace *namespace, const char *name)
2170 type_t *type;
2171 if (!(type = find_type(name, namespace, 0)) &&
2172 !(type = find_type(name, parameters_namespace, 0)))
2174 error_loc("type '%s' not found in %s namespace\n", name, namespace && namespace->name ? namespace->name : "global");
2175 return NULL;
2177 return type;
2180 static struct namespace *find_namespace_or_error(struct namespace *parent, const char *name)
2182 struct namespace *namespace = NULL;
2184 if (!winrt_mode)
2185 error_loc("namespaces are only supported in winrt mode.\n");
2186 else if (!(namespace = find_sub_namespace(parent, name)))
2187 error_loc("namespace '%s' not found in '%s'\n", name, parent->name);
2189 return namespace;
2192 int is_type(const char *name)
2194 return find_type(name, current_namespace, 0) != NULL ||
2195 find_type(name, parameters_namespace, 0);
2198 type_t *get_type(enum type_type type, char *name, struct namespace *namespace, int t)
2200 type_t *tp;
2201 if (!namespace)
2202 namespace = &global_namespace;
2203 if (name) {
2204 tp = find_type(name, namespace, t);
2205 if (tp) {
2206 free(name);
2207 return tp;
2210 tp = make_type(type);
2211 tp->name = name;
2212 tp->namespace = namespace;
2213 if (!name) return tp;
2214 return reg_type(tp, name, namespace, t);
2217 /***** constant repository *****/
2219 struct rconst {
2220 char *name;
2221 var_t *var;
2222 struct rconst *next;
2225 struct rconst *const_hash[HASHMAX];
2227 static var_t *reg_const(var_t *var)
2229 struct rconst *nc;
2230 int hash;
2231 if (!var->name) {
2232 error_loc("registering constant without name\n");
2233 return var;
2235 hash = hash_ident(var->name);
2236 nc = xmalloc(sizeof(struct rconst));
2237 nc->name = var->name;
2238 nc->var = var;
2239 nc->next = const_hash[hash];
2240 const_hash[hash] = nc;
2241 return var;
2244 var_t *find_const(const char *name, int f)
2246 struct rconst *cur = const_hash[hash_ident(name)];
2247 while (cur && strcmp(cur->name, name))
2248 cur = cur->next;
2249 if (!cur) {
2250 if (f) error_loc("constant '%s' not found\n", name);
2251 return NULL;
2253 return cur->var;
2256 char *gen_name(void)
2258 static unsigned long n = 0;
2259 static const char *file_id;
2261 if (! file_id)
2263 char *dst = replace_extension( get_basename(input_idl_name), ".idl", "" );
2264 file_id = dst;
2266 for (; *dst; ++dst)
2267 if (! isalnum((unsigned char) *dst))
2268 *dst = '_';
2270 return strmake("__WIDL_%s_generated_name_%08lX", file_id, n++);
2273 static int is_allowed_conf_type(const type_t *type)
2275 switch (type_get_type(type))
2277 case TYPE_ENUM:
2278 return TRUE;
2279 case TYPE_BASIC:
2280 switch (type_basic_get_type(type))
2282 case TYPE_BASIC_INT8:
2283 case TYPE_BASIC_INT16:
2284 case TYPE_BASIC_INT32:
2285 case TYPE_BASIC_INT64:
2286 case TYPE_BASIC_INT:
2287 case TYPE_BASIC_LONG:
2288 case TYPE_BASIC_CHAR:
2289 case TYPE_BASIC_HYPER:
2290 case TYPE_BASIC_BYTE:
2291 case TYPE_BASIC_WCHAR:
2292 return TRUE;
2293 default:
2294 return FALSE;
2296 case TYPE_ALIAS:
2297 /* shouldn't get here because of type_get_type call above */
2298 assert(0);
2299 /* fall through */
2300 case TYPE_STRUCT:
2301 case TYPE_UNION:
2302 case TYPE_ENCAPSULATED_UNION:
2303 case TYPE_ARRAY:
2304 case TYPE_POINTER:
2305 case TYPE_VOID:
2306 case TYPE_MODULE:
2307 case TYPE_COCLASS:
2308 case TYPE_FUNCTION:
2309 case TYPE_INTERFACE:
2310 case TYPE_BITFIELD:
2311 case TYPE_RUNTIMECLASS:
2312 case TYPE_DELEGATE:
2313 return FALSE;
2314 case TYPE_APICONTRACT:
2315 case TYPE_PARAMETERIZED_TYPE:
2316 case TYPE_PARAMETER:
2317 /* not supposed to be here */
2318 assert(0);
2319 break;
2321 return FALSE;
2324 static int is_ptr_guid_type(const type_t *type)
2326 /* first, make sure it is a pointer to something */
2327 if (!is_ptr(type)) return FALSE;
2329 /* second, make sure it is a pointer to something of size sizeof(GUID),
2330 * i.e. 16 bytes */
2331 return (type_memsize(type_pointer_get_ref_type(type)) == 16);
2334 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2336 expr_t *dim;
2337 struct expr_loc expr_loc;
2338 expr_loc.v = arg;
2339 expr_loc.attr = attr_name;
2340 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2342 if (dim->type != EXPR_VOID)
2344 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2345 if (!is_allowed_conf_type(expr_type))
2346 error_at( &arg->where, "expression must resolve to integral type <= 32bits for attribute %s\n", attr_name );
2351 static void check_remoting_fields(const var_t *var, type_t *type);
2353 /* checks that properties common to fields and arguments are consistent */
2354 static void check_field_common(const type_t *container_type,
2355 const char *container_name, const var_t *arg)
2357 type_t *type = arg->declspec.type;
2358 int more_to_do;
2359 const char *container_type_name;
2360 const char *var_type;
2362 switch (type_get_type(container_type))
2364 case TYPE_STRUCT:
2365 container_type_name = "struct";
2366 var_type = "field";
2367 break;
2368 case TYPE_UNION:
2369 container_type_name = "union";
2370 var_type = "arm";
2371 break;
2372 case TYPE_ENCAPSULATED_UNION:
2373 container_type_name = "encapsulated union";
2374 var_type = "arm";
2375 break;
2376 case TYPE_FUNCTION:
2377 container_type_name = "function";
2378 var_type = "parameter";
2379 break;
2380 default:
2381 /* should be no other container types */
2382 assert(0);
2383 return;
2386 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2387 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->declspec.type, ATTR_STRING)))
2388 error_at( &arg->where, "string and length_is specified for argument %s are mutually exclusive attributes\n", arg->name );
2390 if (is_attr(arg->attrs, ATTR_SIZEIS))
2392 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2393 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2395 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2397 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2398 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2400 if (is_attr(arg->attrs, ATTR_IIDIS))
2402 struct expr_loc expr_loc;
2403 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2404 if (expr->type != EXPR_VOID)
2406 const type_t *expr_type;
2407 expr_loc.v = arg;
2408 expr_loc.attr = "iid_is";
2409 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2410 if (!expr_type || !is_ptr_guid_type(expr_type))
2411 error_at( &arg->where, "expression must resolve to pointer to GUID type for attribute iid_is\n" );
2414 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2416 struct expr_loc expr_loc;
2417 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2418 if (expr->type != EXPR_VOID)
2420 const type_t *expr_type;
2421 expr_loc.v = arg;
2422 expr_loc.attr = "switch_is";
2423 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2424 if (!expr_type || !is_allowed_conf_type(expr_type))
2425 error_at( &arg->where, "expression must resolve to integral type <= 32bits for attribute %s\n", expr_loc.attr );
2431 more_to_do = FALSE;
2433 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
2435 case TGT_STRUCT:
2436 case TGT_UNION:
2437 check_remoting_fields(arg, type);
2438 break;
2439 case TGT_INVALID:
2441 const char *reason = "is invalid";
2442 switch (type_get_type(type))
2444 case TYPE_VOID:
2445 reason = "cannot derive from void *";
2446 break;
2447 case TYPE_FUNCTION:
2448 reason = "cannot be a function pointer";
2449 break;
2450 case TYPE_BITFIELD:
2451 reason = "cannot be a bit-field";
2452 break;
2453 case TYPE_COCLASS:
2454 reason = "cannot be a class";
2455 break;
2456 case TYPE_INTERFACE:
2457 reason = "cannot be a non-pointer to an interface";
2458 break;
2459 case TYPE_MODULE:
2460 reason = "cannot be a module";
2461 break;
2462 default:
2463 break;
2465 error_at( &arg->where, "%s \'%s\' of %s \'%s\' %s\n", var_type, arg->name, container_type_name, container_name, reason );
2466 break;
2468 case TGT_CTXT_HANDLE:
2469 case TGT_CTXT_HANDLE_POINTER:
2470 if (type_get_type(container_type) != TYPE_FUNCTION)
2471 error_at( &arg->where, "%s \'%s\' of %s \'%s\' cannot be a context handle\n",
2472 var_type, arg->name, container_type_name, container_name );
2473 break;
2474 case TGT_STRING:
2476 const type_t *t = type;
2477 while (is_ptr(t))
2478 t = type_pointer_get_ref_type(t);
2479 if (is_aliaschain_attr(t, ATTR_RANGE))
2480 warning_at( &arg->where, "%s: range not verified for a string of ranged types\n", arg->name );
2481 break;
2483 case TGT_POINTER:
2484 if (type_get_type(type_pointer_get_ref_type(type)) != TYPE_VOID ||
2485 !type->name || strcmp(type->name, "HANDLE"))
2487 type = type_pointer_get_ref_type(type);
2488 more_to_do = TRUE;
2490 break;
2491 case TGT_ARRAY:
2492 type = type_array_get_element_type(type);
2493 more_to_do = TRUE;
2494 break;
2495 case TGT_ENUM:
2496 type = type_get_real_type(type);
2497 if (!type_is_complete(type))
2498 error_at( &arg->where, "undefined type declaration \"enum %s\"\n", type->name );
2499 case TGT_USER_TYPE:
2500 case TGT_IFACE_POINTER:
2501 case TGT_BASIC:
2502 case TGT_RANGE:
2503 /* nothing to do */
2504 break;
2506 } while (more_to_do);
2509 static void check_remoting_fields(const var_t *var, type_t *type)
2511 const var_t *field;
2512 const var_list_t *fields = NULL;
2514 type = type_get_real_type(type);
2516 if (type->checked)
2517 return;
2519 type->checked = TRUE;
2521 if (type_get_type(type) == TYPE_STRUCT)
2523 if (type_is_complete(type))
2524 fields = type_struct_get_fields(type);
2525 else
2526 error_at( &var->where, "undefined type declaration \"struct %s\"\n", type->name );
2528 else if (type_get_type(type) == TYPE_UNION || type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2530 if (type_is_complete(type))
2531 fields = type_union_get_cases(type);
2532 else
2533 error_at( &var->where, "undefined type declaration \"union %s\"\n", type->name );
2536 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2537 if (field->declspec.type) check_field_common(type, type->name, field);
2540 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2541 static void check_remoting_args(const var_t *func)
2543 const char *funcname = func->name;
2544 const var_t *arg;
2546 if (!type_function_get_args(func->declspec.type))
2547 return;
2549 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
2551 const type_t *type = arg->declspec.type;
2553 /* check that [out] parameters have enough pointer levels */
2554 if (is_attr(arg->attrs, ATTR_OUT))
2556 switch (typegen_detect_type(type, arg->attrs, TDT_ALL_TYPES))
2558 case TGT_BASIC:
2559 case TGT_ENUM:
2560 case TGT_RANGE:
2561 case TGT_STRUCT:
2562 case TGT_UNION:
2563 case TGT_CTXT_HANDLE:
2564 case TGT_USER_TYPE:
2565 error_at( &arg->where, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname );
2566 break;
2567 case TGT_IFACE_POINTER:
2568 error_at( &arg->where, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname );
2569 break;
2570 case TGT_STRING:
2571 if (is_array(type))
2573 /* needs conformance or fixed dimension */
2574 if (type_array_has_conformance(type) &&
2575 type_array_get_conformance(type)->type != EXPR_VOID) break;
2576 if (!type_array_has_conformance(type) && type_array_get_dim(type)) break;
2578 if (is_attr( arg->attrs, ATTR_IN )) break;
2579 error_at( &arg->where, "out parameter \'%s\' of function \'%s\' cannot be an unsized string\n", arg->name, funcname );
2580 break;
2581 case TGT_INVALID:
2582 /* already error'd before we get here */
2583 case TGT_CTXT_HANDLE_POINTER:
2584 case TGT_POINTER:
2585 case TGT_ARRAY:
2586 /* OK */
2587 break;
2591 check_field_common(func->declspec.type, funcname, arg);
2594 if (type_get_type(type_function_get_rettype(func->declspec.type)) != TYPE_VOID)
2596 var_t var;
2597 var = *func;
2598 var.declspec.type = type_function_get_rettype(func->declspec.type);
2599 var.name = xstrdup("return value");
2600 check_field_common(func->declspec.type, funcname, &var);
2601 free(var.name);
2605 static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func)
2607 unsigned char explicit_fc, implicit_fc;
2609 /* check for a defined binding handle */
2610 if (!get_func_handle_var( iface, func, &explicit_fc, &implicit_fc ) || !explicit_fc)
2612 /* no explicit handle specified so add
2613 * "[in] handle_t IDL_handle" as the first parameter to the
2614 * function */
2615 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2616 idl_handle->attrs = append_attr( NULL, attr_int( iface->where, ATTR_IN, 0 ) );
2617 idl_handle->declspec.type = find_type_or_error(NULL, "handle_t");
2618 type_function_add_head_arg(func->declspec.type, idl_handle);
2622 static void check_functions(const type_t *iface, int is_inside_library)
2624 const statement_t *stmt;
2625 /* check for duplicates */
2626 if (is_attr(iface->attrs, ATTR_DISPINTERFACE))
2628 var_list_t *methods = type_dispiface_get_methods(iface);
2629 var_t *func, *func_iter;
2631 if (methods) LIST_FOR_EACH_ENTRY( func, methods, var_t, entry )
2633 LIST_FOR_EACH_ENTRY( func_iter, methods, var_t, entry )
2635 if (func == func_iter) break;
2636 if (strcmp(func->name, func_iter->name)) continue;
2637 if (is_attr(func->attrs, ATTR_EVENTADD) != is_attr(func_iter->attrs, ATTR_EVENTADD)) continue;
2638 if (is_attr(func->attrs, ATTR_EVENTREMOVE) != is_attr(func_iter->attrs, ATTR_EVENTREMOVE)) continue;
2639 if (is_attr(func->attrs, ATTR_PROPGET) != is_attr(func_iter->attrs, ATTR_PROPGET)) continue;
2640 if (is_attr(func->attrs, ATTR_PROPPUT) != is_attr(func_iter->attrs, ATTR_PROPPUT)) continue;
2641 if (is_attr(func->attrs, ATTR_PROPPUTREF) != is_attr(func_iter->attrs, ATTR_PROPPUTREF)) continue;
2642 error_at( &func->where, "duplicated function \'%s\'\n", func->name );
2646 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
2648 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2650 var_t *func = stmt->u.var;
2651 add_explicit_handle_if_necessary(iface, func);
2654 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2656 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2658 const var_t *func = stmt->u.var;
2659 if (!is_attr(func->attrs, ATTR_LOCAL))
2660 check_remoting_args(func);
2665 static int async_iface_attrs(attr_list_t *attrs, const attr_t *attr)
2667 switch(attr->type)
2669 case ATTR_UUID:
2670 return 0;
2671 case ATTR_ASYNCUUID:
2672 append_attr( attrs, attr_ptr( attr->where, ATTR_UUID, attr->u.pval ) );
2673 return 0;
2674 default:
2675 return 1;
2679 static int arg_in_attrs(attr_list_t *attrs, const attr_t *attr)
2681 return attr->type != ATTR_OUT && attr->type != ATTR_RETVAL;
2684 static int arg_out_attrs(attr_list_t *attrs, const attr_t *attr)
2686 return attr->type != ATTR_IN;
2689 static void check_async_uuid(type_t *iface)
2691 statement_list_t *stmts = NULL;
2692 statement_t *stmt;
2693 type_t *async_iface;
2694 type_t *inherit;
2696 if (!is_attr(iface->attrs, ATTR_ASYNCUUID)) return;
2698 inherit = type_iface_get_inherit(iface);
2699 if (inherit && strcmp(inherit->name, "IUnknown"))
2700 inherit = type_iface_get_async_iface(inherit);
2701 if (!inherit)
2702 error_loc("async_uuid applied to an interface with incompatible parent\n");
2704 async_iface = type_interface_declare(strmake("Async%s", iface->name), iface->namespace);
2706 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2708 var_t *begin_func, *finish_func, *func = stmt->u.var, *arg;
2709 var_list_t *begin_args = NULL, *finish_args = NULL, *args;
2711 if (is_attr(func->attrs, ATTR_CALLAS)) continue;
2713 args = type_function_get_args(func->declspec.type);
2714 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
2716 if (is_attr(arg->attrs, ATTR_IN) || !is_attr(arg->attrs, ATTR_OUT))
2717 begin_args = append_var(begin_args, copy_var(arg, xstrdup(arg->name), arg_in_attrs));
2718 if (is_attr(arg->attrs, ATTR_OUT))
2719 finish_args = append_var(finish_args, copy_var(arg, xstrdup(arg->name), arg_out_attrs));
2722 begin_func = copy_var(func, strmake("Begin_%s", func->name), NULL);
2723 begin_func->declspec.type = type_new_function(begin_args);
2724 begin_func->declspec.type->attrs = func->attrs;
2725 begin_func->declspec.type->details.function->retval = func->declspec.type->details.function->retval;
2726 stmts = append_statement(stmts, make_statement_declaration(begin_func));
2728 finish_func = copy_var(func, strmake("Finish_%s", func->name), NULL);
2729 finish_func->declspec.type = type_new_function(finish_args);
2730 finish_func->declspec.type->attrs = func->attrs;
2731 finish_func->declspec.type->details.function->retval = func->declspec.type->details.function->retval;
2732 stmts = append_statement(stmts, make_statement_declaration(finish_func));
2735 type_interface_define(async_iface, map_attrs(iface->attrs, async_iface_attrs), inherit, stmts, NULL);
2736 iface->details.iface->async_iface = async_iface->details.iface->async_iface = async_iface;
2739 static statement_list_t *append_parameterized_type_stmts(statement_list_t *stmts)
2741 statement_t *stmt, *next;
2743 if (stmts && parameterized_type_stmts) LIST_FOR_EACH_ENTRY_SAFE(stmt, next, parameterized_type_stmts, statement_t, entry)
2745 switch(stmt->type)
2747 case STMT_TYPE:
2748 stmt->u.type = type_parameterized_type_specialize_define(stmt->u.type);
2749 stmt->declonly = FALSE;
2750 list_remove(&stmt->entry);
2751 stmts = append_statement(stmts, stmt);
2752 break;
2753 default:
2754 assert(0); /* should not be there */
2755 break;
2759 return stmts;
2762 static void check_statements(const statement_list_t *stmts, int is_inside_library)
2764 const statement_t *stmt;
2766 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2768 switch(stmt->type) {
2769 case STMT_LIBRARY:
2770 check_statements(stmt->u.lib->stmts, TRUE);
2771 break;
2772 case STMT_TYPE:
2773 switch(type_get_type(stmt->u.type)) {
2774 case TYPE_INTERFACE:
2775 check_functions(stmt->u.type, is_inside_library);
2776 break;
2777 case TYPE_COCLASS:
2778 if(winrt_mode)
2779 error_loc("coclass is not allowed in Windows Runtime mode\n");
2780 break;
2781 default:
2782 break;
2784 break;
2785 default:
2786 break;
2791 static void check_all_user_types(const statement_list_t *stmts)
2793 const statement_t *stmt;
2794 const var_t *v;
2796 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2798 if (stmt->type == STMT_LIBRARY)
2799 check_all_user_types(stmt->u.lib->stmts);
2800 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE &&
2801 !is_local(stmt->u.type->attrs))
2803 const statement_t *stmt_func;
2804 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type)) {
2805 const var_t *func = stmt_func->u.var;
2806 if (type_function_get_args(func->declspec.type))
2807 LIST_FOR_EACH_ENTRY( v, type_function_get_args(func->declspec.type), const var_t, entry )
2808 check_for_additional_prototype_types(v->declspec.type);
2809 check_for_additional_prototype_types(type_function_get_rettype(func->declspec.type));
2815 static statement_t *make_statement(enum statement_type type)
2817 statement_t *stmt = xmalloc(sizeof(*stmt));
2818 stmt->type = type;
2819 return stmt;
2822 static statement_t *make_statement_type_decl(type_t *type)
2824 statement_t *stmt = make_statement(STMT_TYPE);
2825 stmt->u.type = type;
2826 stmt->declonly = !type->defined;
2827 return stmt;
2830 static statement_t *make_statement_reference(type_t *type)
2832 statement_t *stmt = make_statement(STMT_TYPEREF);
2833 stmt->u.type = type;
2834 return stmt;
2837 static statement_t *make_statement_declaration(var_t *var)
2839 statement_t *stmt = make_statement(STMT_DECLARATION);
2840 stmt->u.var = var;
2841 if (var->declspec.stgclass == STG_EXTERN && var->eval)
2842 warning("'%s' initialised and declared extern\n", var->name);
2843 if (is_const_decl(var))
2845 if (var->eval)
2846 reg_const(var);
2848 else if (type_get_type(var->declspec.type) == TYPE_FUNCTION)
2849 check_function_attrs(var->name, var->attrs);
2850 else if (var->declspec.stgclass == STG_NONE || var->declspec.stgclass == STG_REGISTER)
2851 error_loc("instantiation of data is illegal\n");
2852 return stmt;
2855 static statement_t *make_statement_library(typelib_t *typelib)
2857 statement_t *stmt = make_statement(STMT_LIBRARY);
2858 stmt->u.lib = typelib;
2859 return stmt;
2862 static statement_t *make_statement_pragma(const char *str)
2864 statement_t *stmt = make_statement(STMT_PRAGMA);
2865 stmt->u.str = str;
2866 return stmt;
2869 static statement_t *make_statement_cppquote(const char *str)
2871 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2872 stmt->u.str = str;
2873 return stmt;
2876 static statement_t *make_statement_importlib(const char *str)
2878 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2879 stmt->u.str = str;
2880 return stmt;
2883 static statement_t *make_statement_import(const char *str)
2885 statement_t *stmt = make_statement(STMT_IMPORT);
2886 stmt->u.str = str;
2887 return stmt;
2890 static statement_t *make_statement_module(type_t *type)
2892 statement_t *stmt = make_statement(STMT_MODULE);
2893 stmt->u.type = type;
2894 return stmt;
2897 static statement_t *make_statement_typedef(declarator_list_t *decls, int declonly)
2899 declarator_t *decl, *next;
2900 statement_t *stmt;
2902 if (!decls) return NULL;
2904 stmt = make_statement(STMT_TYPEDEF);
2905 stmt->u.type_list = NULL;
2906 stmt->declonly = declonly;
2908 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2910 var_t *var = decl->var;
2911 type_t *type = find_type_or_error(current_namespace, var->name);
2912 stmt->u.type_list = append_typeref(stmt->u.type_list, make_typeref(type));
2913 free(decl);
2914 free(var);
2917 return stmt;
2920 static statement_t *make_statement_parameterized_type(type_t *type, typeref_list_t *params)
2922 statement_t *stmt = make_statement(STMT_TYPE);
2923 stmt->u.type = type_parameterized_type_specialize_partial(type, params);
2924 return stmt;
2927 static statement_t *make_statement_delegate(type_t *ret, var_list_t *args)
2929 declarator_t *decl = make_declarator(make_var(xstrdup("Invoke")));
2930 decl_spec_t *spec = make_decl_spec(ret, NULL, NULL, STG_NONE, 0, 0);
2931 append_chain_type(decl, type_new_function(args), 0);
2932 return make_statement_declaration(declare_var(NULL, spec, decl, FALSE));
2935 static statement_list_t *append_statements(statement_list_t *l1, statement_list_t *l2)
2937 if (!l2) return l1;
2938 if (!l1 || l1 == l2) return l2;
2939 list_move_tail (l1, l2);
2940 return l1;
2943 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2945 if (!stmt) return list;
2946 if (!list)
2948 list = xmalloc( sizeof(*list) );
2949 list_init( list );
2951 list_add_tail( list, &stmt->entry );
2952 return list;
2955 type_t *find_parameterized_type(type_t *type, typeref_list_t *params)
2957 char *name = format_parameterized_type_name(type, params);
2959 if (parameters_namespace)
2961 assert(type->type_type == TYPE_PARAMETERIZED_TYPE);
2962 type = type_parameterized_type_specialize_partial(type, params);
2964 else if ((type = find_type(name, type->namespace, 0)))
2965 assert(type->type_type != TYPE_PARAMETERIZED_TYPE);
2966 else
2967 error_loc("parameterized type '%s' not declared\n", name);
2969 free(name);
2970 return type;