crypt32/test: When using dynamic binding for functions, do it in all locations.
[wine/multimedia.git] / tools / widl / parser.y
blob2ffa51531bbec93eec71c4979fb0c748ad624273
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 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
63 # endif
64 #endif
66 #define YYERROR_VERBOSE
68 static unsigned char pointer_default = RPC_FC_UP;
70 typedef struct list typelist_t;
71 struct typenode {
72 type_t *type;
73 struct list entry;
76 struct _import_t
78 char *name;
79 int import_performed;
82 typedef struct _decl_spec_t
84 type_t *type;
85 attr_list_t *attrs;
86 enum storage_class stgclass;
87 } decl_spec_t;
89 typelist_t incomplete_types = LIST_INIT(incomplete_types);
91 static void fix_incomplete(void);
92 static void fix_incomplete_types(type_t *complete_type);
94 static str_list_t *append_str(str_list_t *list, char *str);
95 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
96 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
97 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass);
98 static attr_t *make_attr(enum attr_type type);
99 static attr_t *make_attrv(enum attr_type type, unsigned int val);
100 static attr_t *make_attrp(enum attr_type type, void *val);
101 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
102 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
103 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl, int top);
104 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
105 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
106 static ifref_t *make_ifref(type_t *iface);
107 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
108 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
109 static declarator_t *make_declarator(var_t *var);
110 static type_t *make_safearray(type_t *type);
111 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
112 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
114 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
115 static type_t *find_type_or_error(const char *name, int t);
116 static type_t *find_type_or_error2(char *name, int t);
118 static var_t *reg_const(var_t *var);
120 static char *gen_name(void);
121 static void check_arg_attrs(const var_t *arg);
122 static void check_statements(const statement_list_t *stmts, int is_inside_library);
123 static void check_all_user_types(const statement_list_t *stmts);
124 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
125 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
126 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
127 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
128 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
129 static attr_list_t *check_union_attrs(attr_list_t *attrs);
130 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
131 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
132 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
133 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
134 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
135 const char *get_attr_display_name(enum attr_type type);
136 static void add_explicit_handle_if_necessary(var_t *func);
137 static void check_def(const type_t *t);
139 static statement_t *make_statement(enum statement_type type);
140 static statement_t *make_statement_type_decl(type_t *type);
141 static statement_t *make_statement_reference(type_t *type);
142 static statement_t *make_statement_declaration(var_t *var);
143 static statement_t *make_statement_library(typelib_t *typelib);
144 static statement_t *make_statement_cppquote(const char *str);
145 static statement_t *make_statement_importlib(const char *str);
146 static statement_t *make_statement_module(type_t *type);
147 static statement_t *make_statement_typedef(var_list_t *names);
148 static statement_t *make_statement_import(const char *str);
149 static statement_t *make_statement_typedef(var_list_t *names);
150 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
153 %union {
154 attr_t *attr;
155 attr_list_t *attr_list;
156 str_list_t *str_list;
157 expr_t *expr;
158 expr_list_t *expr_list;
159 array_dims_t *array_dims;
160 type_t *type;
161 var_t *var;
162 var_list_t *var_list;
163 declarator_t *declarator;
164 declarator_list_t *declarator_list;
165 statement_t *statement;
166 statement_list_t *stmt_list;
167 ifref_t *ifref;
168 ifref_list_t *ifref_list;
169 char *str;
170 UUID *uuid;
171 unsigned int num;
172 double dbl;
173 interface_info_t ifinfo;
174 typelib_t *typelib;
175 struct _import_t *import;
176 struct _decl_spec_t *declspec;
177 enum storage_class stgclass;
180 %token <str> aIDENTIFIER
181 %token <str> aKNOWNTYPE
182 %token <num> aNUM aHEXNUM
183 %token <dbl> aDOUBLE
184 %token <str> aSTRING aWSTRING aSQSTRING
185 %token <uuid> aUUID
186 %token aEOF
187 %token SHL SHR
188 %token MEMBERPTR
189 %token EQUALITY INEQUALITY
190 %token GREATEREQUAL LESSEQUAL
191 %token LOGICALOR LOGICALAND
192 %token ELLIPSIS
193 %token tAGGREGATABLE tALLOCATE tANNOTATION tAPPOBJECT tASYNC tASYNCUUID
194 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
195 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
196 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
197 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
198 %token tDECODE tDEFAULT tDEFAULTBIND
199 %token tDEFAULTCOLLELEM
200 %token tDEFAULTVALUE
201 %token tDEFAULTVTABLE
202 %token tDISABLECONSISTENCYCHECK tDISPLAYBIND
203 %token tDISPINTERFACE
204 %token tDLLNAME tDOUBLE tDUAL
205 %token tENABLEALLOCATE tENCODE tENDPOINT
206 %token tENTRY tENUM tERRORSTATUST
207 %token tEXPLICITHANDLE tEXTERN
208 %token tFALSE
209 %token tFASTCALL tFAULTSTATUS
210 %token tFLOAT tFORCEALLOCATE
211 %token tHANDLE
212 %token tHANDLET
213 %token tHELPCONTEXT tHELPFILE
214 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
215 %token tHIDDEN
216 %token tHYPER tID tIDEMPOTENT
217 %token tIGNORE tIIDIS
218 %token tIMMEDIATEBIND
219 %token tIMPLICITHANDLE
220 %token tIMPORT tIMPORTLIB
221 %token tIN tIN_LINE tINLINE
222 %token tINPUTSYNC
223 %token tINT tINT3264 tINT64
224 %token tINTERFACE
225 %token tLCID
226 %token tLENGTHIS tLIBRARY
227 %token tLICENSED tLOCAL
228 %token tLONG
229 %token tMAYBE tMESSAGE
230 %token tMETHODS
231 %token tMODULE
232 %token tNOCODE tNONBROWSABLE
233 %token tNONCREATABLE
234 %token tNONEXTENSIBLE
235 %token tNOTIFY tNOTIFYFLAG
236 %token tNULL
237 %token tOBJECT tODL tOLEAUTOMATION
238 %token tOPTIMIZE tOPTIONAL
239 %token tOUT
240 %token tPARTIALIGNORE tPASCAL
241 %token tPOINTERDEFAULT
242 %token tPROPERTIES
243 %token tPROPGET tPROPPUT tPROPPUTREF
244 %token tPROXY tPTR
245 %token tPUBLIC
246 %token tRANGE
247 %token tREADONLY tREF
248 %token tREGISTER tREPRESENTAS
249 %token tREQUESTEDIT
250 %token tRESTRICTED
251 %token tRETVAL
252 %token tSAFEARRAY
253 %token tSHORT
254 %token tSIGNED
255 %token tSIZEIS tSIZEOF
256 %token tSMALL
257 %token tSOURCE
258 %token tSTATIC
259 %token tSTDCALL
260 %token tSTRICTCONTEXTHANDLE
261 %token tSTRING tSTRUCT
262 %token tSWITCH tSWITCHIS tSWITCHTYPE
263 %token tTRANSMITAS
264 %token tTRUE
265 %token tTYPEDEF
266 %token tUIDEFAULT tUNION
267 %token tUNIQUE
268 %token tUNSIGNED
269 %token tUSESGETLASTERROR tUSERMARSHAL tUUID
270 %token tV1ENUM
271 %token tVARARG
272 %token tVERSION
273 %token tVOID
274 %token tWCHAR tWIREMARSHAL
276 %type <attr> attribute type_qualifier function_specifier
277 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
278 %type <str_list> str_list
279 %type <expr> m_expr expr expr_const expr_int_const array m_bitfield
280 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
281 %type <ifinfo> interfacehdr
282 %type <stgclass> storage_cls_spec
283 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
284 %type <type> inherit interface interfacedef interfacedec
285 %type <type> dispinterface dispinterfacehdr dispinterfacedef
286 %type <type> module modulehdr moduledef
287 %type <type> base_type int_std
288 %type <type> enumdef structdef uniondef typedecl
289 %type <type> type
290 %type <ifref> coclass_int
291 %type <ifref_list> coclass_ints
292 %type <var> arg ne_union_field union_field s_field case enum declaration
293 %type <var> funcdef
294 %type <var_list> m_args arg_list args dispint_meths
295 %type <var_list> fields ne_union_fields cases enums enum_list dispint_props field
296 %type <var> m_ident ident
297 %type <declarator> declarator direct_declarator init_declarator struct_declarator
298 %type <declarator> m_any_declarator any_declarator any_declarator_no_direct any_direct_declarator
299 %type <declarator> m_abstract_declarator abstract_declarator abstract_declarator_no_direct abstract_direct_declarator
300 %type <declarator_list> declarator_list struct_declarator_list
301 %type <type> coclass coclasshdr coclassdef
302 %type <num> pointer_type version
303 %type <str> libraryhdr callconv cppquote importlib import t_ident
304 %type <uuid> uuid_string
305 %type <import> import_start
306 %type <typelib> library_start librarydef
307 %type <statement> statement typedef
308 %type <stmt_list> gbl_statements imp_statements int_statements
310 %left ','
311 %right '?' ':'
312 %left LOGICALOR
313 %left LOGICALAND
314 %left '|'
315 %left '^'
316 %left '&'
317 %left EQUALITY INEQUALITY
318 %left '<' '>' LESSEQUAL GREATEREQUAL
319 %left SHL SHR
320 %left '-' '+'
321 %left '*' '/' '%'
322 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
323 %left '.' MEMBERPTR '[' ']'
327 input: gbl_statements { fix_incomplete();
328 check_statements($1, FALSE);
329 check_all_user_types($1);
330 write_header($1);
331 write_id_data($1);
332 write_proxies($1);
333 write_client($1);
334 write_server($1);
335 write_dlldata($1);
336 write_local_stubs($1);
340 gbl_statements: { $$ = NULL; }
341 | gbl_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
342 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
343 | gbl_statements coclass ';' { $$ = $1;
344 reg_type($2, $2->name, 0);
346 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
347 reg_type($2, $2->name, 0);
349 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
350 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
351 | gbl_statements statement { $$ = append_statement($1, $2); }
354 imp_statements: { $$ = NULL; }
355 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
356 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
357 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); }
358 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
359 reg_type($2, $2->name, 0);
361 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
362 | imp_statements statement { $$ = append_statement($1, $2); }
363 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
364 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
367 int_statements: { $$ = NULL; }
368 | int_statements statement { $$ = append_statement($1, $2); }
371 semicolon_opt:
372 | ';'
375 statement:
376 cppquote { $$ = make_statement_cppquote($1); }
377 | typedecl ';' { $$ = make_statement_type_decl($1); }
378 | declaration ';' { $$ = make_statement_declaration($1); }
379 | import { $$ = make_statement_import($1); }
380 | typedef ';' { $$ = $1; }
383 typedecl:
384 enumdef
385 | structdef
386 | uniondef
387 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
388 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
389 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
392 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
394 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
395 $$ = xmalloc(sizeof(struct _import_t));
396 $$->name = $2;
397 $$->import_performed = do_import($2);
398 if (!$$->import_performed) yychar = aEOF;
402 import: import_start imp_statements aEOF { $$ = $1->name;
403 if ($1->import_performed) pop_import();
404 free($1);
408 importlib: tIMPORTLIB '(' aSTRING ')'
409 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
412 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
414 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
415 if (!parse_only) start_typelib($$);
418 librarydef: library_start imp_statements '}'
419 semicolon_opt { $$ = $1;
420 $$->stmts = $2;
421 if (!parse_only) end_typelib();
425 m_args: { $$ = NULL; }
426 | args
429 arg_list: arg { check_arg_attrs($1); $$ = append_var( NULL, $1 ); }
430 | arg_list ',' arg { check_arg_attrs($3); $$ = append_var( $1, $3 ); }
433 args: arg_list
434 | arg_list ',' ELLIPSIS { $$ = append_var( $1, make_var(strdup("...")) ); }
437 /* split into two rules to get bison to resolve a tVOID conflict */
438 arg: attributes decl_spec m_any_declarator { if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
439 error_loc("invalid storage class for function parameter\n");
440 $$ = declare_var($1, $2, $3, TRUE);
441 free($2); free($3);
443 | decl_spec m_any_declarator { if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
444 error_loc("invalid storage class for function parameter\n");
445 $$ = declare_var(NULL, $1, $2, TRUE);
446 free($1); free($2);
450 array: '[' m_expr ']' { $$ = $2; }
451 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
454 m_attributes: { $$ = NULL; }
455 | attributes
458 attributes:
459 '[' attrib_list ']' { $$ = $2; }
462 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
463 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
464 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
467 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
468 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
471 attribute: { $$ = NULL; }
472 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
473 | tANNOTATION '(' aSTRING ')' { $$ = make_attrp(ATTR_ANNOTATION, $3); }
474 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
475 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
476 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
477 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
478 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
479 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
480 | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
481 | tCODE { $$ = make_attr(ATTR_CODE); }
482 | tCOMMSTATUS { $$ = make_attr(ATTR_COMMSTATUS); }
483 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
484 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
485 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
486 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
487 | tDECODE { $$ = make_attr(ATTR_DECODE); }
488 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
489 | tDEFAULTBIND { $$ = make_attr(ATTR_DEFAULTBIND); }
490 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
491 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
492 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
493 | tDISABLECONSISTENCYCHECK { $$ = make_attr(ATTR_DISABLECONSISTENCYCHECK); }
494 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
495 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
496 | tDUAL { $$ = make_attr(ATTR_DUAL); }
497 | tENABLEALLOCATE { $$ = make_attr(ATTR_ENABLEALLOCATE); }
498 | tENCODE { $$ = make_attr(ATTR_ENCODE); }
499 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
500 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
501 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
502 | tFAULTSTATUS { $$ = make_attr(ATTR_FAULTSTATUS); }
503 | tFORCEALLOCATE { $$ = make_attr(ATTR_FORCEALLOCATE); }
504 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
505 | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
506 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
507 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
508 | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
509 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
510 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
511 | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
512 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
513 | tIGNORE { $$ = make_attr(ATTR_IGNORE); }
514 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
515 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
516 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
517 | tIN { $$ = make_attr(ATTR_IN); }
518 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
519 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
520 | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
521 | tLCID { $$ = make_attr(ATTR_PARAMLCID); }
522 | tLICENSED { $$ = make_attr(ATTR_LICENSED); }
523 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
524 | tMAYBE { $$ = make_attr(ATTR_MAYBE); }
525 | tMESSAGE { $$ = make_attr(ATTR_MESSAGE); }
526 | tNOCODE { $$ = make_attr(ATTR_NOCODE); }
527 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
528 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
529 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
530 | tNOTIFY { $$ = make_attr(ATTR_NOTIFY); }
531 | tNOTIFYFLAG { $$ = make_attr(ATTR_NOTIFYFLAG); }
532 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
533 | tODL { $$ = make_attr(ATTR_ODL); }
534 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
535 | tOPTIMIZE '(' aSTRING ')' { $$ = make_attrp(ATTR_OPTIMIZE, $3); }
536 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
537 | tOUT { $$ = make_attr(ATTR_OUT); }
538 | tPARTIALIGNORE { $$ = make_attr(ATTR_PARTIALIGNORE); }
539 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
540 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
541 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
542 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
543 | tPROXY { $$ = make_attr(ATTR_PROXY); }
544 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
545 | tRANGE '(' expr_int_const ',' expr_int_const ')'
546 { expr_list_t *list = append_expr( NULL, $3 );
547 list = append_expr( list, $5 );
548 $$ = make_attrp(ATTR_RANGE, list); }
549 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
550 | tREPRESENTAS '(' type ')' { $$ = make_attrp(ATTR_REPRESENTAS, $3); }
551 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
552 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
553 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
554 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
555 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
556 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
557 | tSTRING { $$ = make_attr(ATTR_STRING); }
558 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
559 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
560 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
561 | tUIDEFAULT { $$ = make_attr(ATTR_UIDEFAULT); }
562 | tUSESGETLASTERROR { $$ = make_attr(ATTR_USESGETLASTERROR); }
563 | tUSERMARSHAL '(' type ')' { $$ = make_attrp(ATTR_USERMARSHAL, $3); }
564 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
565 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
566 | tVARARG { $$ = make_attr(ATTR_VARARG); }
567 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
568 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
569 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
572 uuid_string:
573 aUUID
574 | aSTRING { if (!is_valid_uuid($1))
575 error_loc("invalid UUID: %s\n", $1);
576 $$ = parse_uuid($1); }
579 callconv: tCDECL { $$ = $<str>1; }
580 | tFASTCALL { $$ = $<str>1; }
581 | tPASCAL { $$ = $<str>1; }
582 | tSTDCALL { $$ = $<str>1; }
585 cases: { $$ = NULL; }
586 | cases case { $$ = append_var( $1, $2 ); }
589 case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
590 $$ = $4; if (!$$) $$ = make_var(NULL);
591 $$->attrs = append_attr( $$->attrs, a );
593 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
594 $$ = $3; if (!$$) $$ = make_var(NULL);
595 $$->attrs = append_attr( $$->attrs, a );
599 enums: { $$ = NULL; }
600 | enum_list ',' { $$ = $1; }
601 | enum_list
604 enum_list: enum { if (!$1->eval)
605 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
606 $$ = append_var( NULL, $1 );
608 | enum_list ',' enum { if (!$3->eval)
610 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
611 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
613 $$ = append_var( $1, $3 );
617 enum: ident '=' expr_int_const { $$ = reg_const($1);
618 $$->eval = $3;
619 $$->type = type_new_int(TYPE_BASIC_INT, 0);
621 | ident { $$ = reg_const($1);
622 $$->type = type_new_int(TYPE_BASIC_INT, 0);
626 enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, TRUE, $4); }
629 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
630 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
634 exprs: { $$ = make_expr(EXPR_VOID); }
635 | expr_list
638 expr_list: expr
639 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
643 m_expr: { $$ = make_expr(EXPR_VOID); }
644 | expr
647 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
648 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
649 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
650 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
651 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
652 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
653 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
654 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
655 | aSQSTRING { $$ = make_exprs(EXPR_CHARCONST, $1); }
656 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
657 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
658 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
659 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
660 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
661 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
662 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
663 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
664 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
665 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
666 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
667 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
668 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
669 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
670 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
671 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
672 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
673 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
674 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
675 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
676 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
677 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
678 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
679 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
680 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
681 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
682 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
683 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
684 | '(' decl_spec m_abstract_declarator ')' expr %prec CAST
685 { $$ = make_exprt(EXPR_CAST, declare_var(NULL, $2, $3, 0), $5); free($2); free($3); }
686 | tSIZEOF '(' decl_spec m_abstract_declarator ')'
687 { $$ = make_exprt(EXPR_SIZEOF, declare_var(NULL, $3, $4, 0), NULL); free($3); free($4); }
688 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
689 | '(' expr ')' { $$ = $2; }
692 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
693 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
696 expr_int_const: expr { $$ = $1;
697 if (!$$->is_const)
698 error_loc("expression is not an integer constant\n");
702 expr_const: expr { $$ = $1;
703 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
704 error_loc("expression is not constant\n");
708 fields: { $$ = NULL; }
709 | fields field { $$ = append_var_list($1, $2); }
712 field: m_attributes decl_spec struct_declarator_list ';'
713 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
714 check_field_attrs(first, $1);
715 $$ = set_var_types($1, $2, $3);
717 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
718 v->type = $2; v->attrs = $1;
719 $$ = append_var(NULL, v);
723 ne_union_field:
724 s_field ';' { $$ = $1; }
725 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
728 ne_union_fields: { $$ = NULL; }
729 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
732 union_field:
733 s_field ';' { $$ = $1; }
734 | ';' { $$ = NULL; }
737 s_field: m_attributes decl_spec declarator { $$ = declare_var(check_field_attrs($3->var->name, $1),
738 $2, $3, FALSE);
739 free($3);
743 funcdef: declaration { $$ = $1;
744 if (type_get_type($$->type) != TYPE_FUNCTION)
745 error_loc("only methods may be declared inside the methods section of a dispinterface\n");
746 check_function_attrs($$->name, $$->attrs);
750 declaration:
751 attributes decl_spec init_declarator
752 { $$ = declare_var($1, $2, $3, FALSE);
753 free($3);
755 | decl_spec init_declarator { $$ = declare_var(NULL, $1, $2, FALSE);
756 free($2);
760 m_ident: { $$ = NULL; }
761 | ident
764 t_ident: { $$ = NULL; }
765 | aIDENTIFIER { $$ = $1; }
766 | aKNOWNTYPE { $$ = $1; }
769 ident: aIDENTIFIER { $$ = make_var($1); }
770 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
771 | aKNOWNTYPE { $$ = make_var($<str>1); }
774 base_type: tBYTE { $$ = find_type_or_error($<str>1, 0); }
775 | tWCHAR { $$ = find_type_or_error($<str>1, 0); }
776 | int_std
777 | tSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), -1); }
778 | tUNSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), 1); }
779 | tUNSIGNED { $$ = type_new_int(TYPE_BASIC_INT, 1); }
780 | tFLOAT { $$ = find_type_or_error($<str>1, 0); }
781 | tDOUBLE { $$ = find_type_or_error($<str>1, 0); }
782 | tBOOLEAN { $$ = find_type_or_error($<str>1, 0); }
783 | tERRORSTATUST { $$ = find_type_or_error($<str>1, 0); }
784 | tHANDLET { $$ = find_type_or_error($<str>1, 0); }
787 m_int:
788 | tINT
791 int_std: tINT { $$ = type_new_int(TYPE_BASIC_INT, 0); }
792 | tSHORT m_int { $$ = type_new_int(TYPE_BASIC_INT16, 0); }
793 | tSMALL { $$ = type_new_int(TYPE_BASIC_INT8, 0); }
794 | tLONG m_int { $$ = type_new_int(TYPE_BASIC_INT32, 0); }
795 | tHYPER m_int { $$ = type_new_int(TYPE_BASIC_HYPER, 0); }
796 | tINT64 { $$ = type_new_int(TYPE_BASIC_INT64, 0); }
797 | tCHAR { $$ = type_new_int(TYPE_BASIC_CHAR, 0); }
798 | tINT3264 { $$ = type_new_int(TYPE_BASIC_INT3264, 0); }
801 coclass: tCOCLASS aIDENTIFIER { $$ = type_new_coclass($2); }
802 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
803 if (type_get_type_detect_alias($$) != TYPE_COCLASS)
804 error_loc("%s was not declared a coclass at %s:%d\n",
805 $2, $$->loc_info.input_name,
806 $$->loc_info.line_number);
810 coclasshdr: attributes coclass { $$ = $2;
811 check_def($$);
812 $$->attrs = check_coclass_attrs($2->name, $1);
816 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
817 { $$ = type_coclass_define($1, $3); }
820 coclass_ints: { $$ = NULL; }
821 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
824 coclass_int:
825 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
828 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, 0); }
829 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, 0); }
832 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
833 $$ = $2;
834 check_def($$);
835 attrs = make_attr(ATTR_DISPINTERFACE);
836 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
837 $$->defined = TRUE;
841 dispint_props: tPROPERTIES ':' { $$ = NULL; }
842 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
845 dispint_meths: tMETHODS ':' { $$ = NULL; }
846 | dispint_meths funcdef ';' { $$ = append_var( $1, $2 ); }
849 dispinterfacedef: dispinterfacehdr '{'
850 dispint_props
851 dispint_meths
852 '}' { $$ = $1;
853 type_dispinterface_define($$, $3, $4);
855 | dispinterfacehdr
856 '{' interface ';' '}' { $$ = $1;
857 type_dispinterface_define_from_iface($$, $3);
861 inherit: { $$ = NULL; }
862 | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
865 interface: tINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, 0); }
866 | tINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, 0); }
869 interfacehdr: attributes interface { $$.interface = $2;
870 $$.old_pointer_default = pointer_default;
871 if (is_attr($1, ATTR_POINTERDEFAULT))
872 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
873 check_def($2);
874 $2->attrs = check_iface_attrs($2->name, $1);
875 $2->defined = TRUE;
879 interfacedef: interfacehdr inherit
880 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
881 type_interface_define($$, $2, $4);
882 pointer_default = $1.old_pointer_default;
884 /* MIDL is able to import the definition of a base class from inside the
885 * definition of a derived class, I'll try to support it with this rule */
886 | interfacehdr ':' aIDENTIFIER
887 '{' import int_statements '}'
888 semicolon_opt { $$ = $1.interface;
889 type_interface_define($$, find_type_or_error2($3, 0), $6);
890 pointer_default = $1.old_pointer_default;
892 | dispinterfacedef semicolon_opt { $$ = $1; }
895 interfacedec:
896 interface ';' { $$ = $1; }
897 | dispinterface ';' { $$ = $1; }
900 module: tMODULE aIDENTIFIER { $$ = type_new_module($2); }
901 | tMODULE aKNOWNTYPE { $$ = type_new_module($2); }
904 modulehdr: attributes module { $$ = $2;
905 $$->attrs = check_module_attrs($2->name, $1);
909 moduledef: modulehdr '{' int_statements '}'
910 semicolon_opt { $$ = $1;
911 type_module_define($$, $3);
915 storage_cls_spec:
916 tEXTERN { $$ = STG_EXTERN; }
917 | tSTATIC { $$ = STG_STATIC; }
918 | tREGISTER { $$ = STG_REGISTER; }
921 function_specifier:
922 tINLINE { $$ = make_attr(ATTR_INLINE); }
925 type_qualifier:
926 tCONST { $$ = make_attr(ATTR_CONST); }
929 m_type_qual_list: { $$ = NULL; }
930 | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
933 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
934 | decl_spec_no_type type m_decl_spec_no_type
935 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
938 m_decl_spec_no_type: { $$ = NULL; }
939 | decl_spec_no_type
942 decl_spec_no_type:
943 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
944 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
945 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
948 declarator:
949 '*' m_type_qual_list declarator %prec PPTR
950 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
951 | callconv declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
952 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
953 | direct_declarator
956 direct_declarator:
957 ident { $$ = make_declarator($1); }
958 | '(' declarator ')' { $$ = $2; }
959 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
960 | direct_declarator '(' m_args ')' { $$ = $1;
961 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
962 $$->type = NULL;
966 /* abstract declarator */
967 abstract_declarator:
968 '*' m_type_qual_list m_abstract_declarator %prec PPTR
969 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
970 | callconv m_abstract_declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
971 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
972 | abstract_direct_declarator
975 /* abstract declarator without accepting direct declarator */
976 abstract_declarator_no_direct:
977 '*' m_type_qual_list m_any_declarator %prec PPTR
978 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
979 | callconv m_any_declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
980 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
983 /* abstract declarator or empty */
984 m_abstract_declarator: { $$ = make_declarator(NULL); }
985 | abstract_declarator
988 /* abstract direct declarator */
989 abstract_direct_declarator:
990 '(' abstract_declarator_no_direct ')' { $$ = $2; }
991 | abstract_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
992 | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
993 | '(' m_args ')'
994 { $$ = make_declarator(NULL);
995 $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
996 $$->type = NULL;
998 | abstract_direct_declarator '(' m_args ')'
999 { $$ = $1;
1000 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
1001 $$->type = NULL;
1005 /* abstract or non-abstract declarator */
1006 any_declarator:
1007 '*' m_type_qual_list m_any_declarator %prec PPTR
1008 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
1009 | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1010 | any_direct_declarator
1013 /* abstract or non-abstract declarator without accepting direct declarator */
1014 any_declarator_no_direct:
1015 '*' m_type_qual_list m_any_declarator %prec PPTR
1016 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
1017 | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1020 /* abstract or non-abstract declarator or empty */
1021 m_any_declarator: { $$ = make_declarator(NULL); }
1022 | any_declarator
1025 /* abstract or non-abstract direct declarator. note: direct declarators
1026 * aren't accepted inside brackets to avoid ambiguity with the rule for
1027 * function arguments */
1028 any_direct_declarator:
1029 ident { $$ = make_declarator($1); }
1030 | '(' any_declarator_no_direct ')' { $$ = $2; }
1031 | any_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
1032 | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
1033 | '(' m_args ')'
1034 { $$ = make_declarator(NULL);
1035 $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
1036 $$->type = NULL;
1038 | any_direct_declarator '(' m_args ')'
1039 { $$ = $1;
1040 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
1041 $$->type = NULL;
1045 declarator_list:
1046 declarator { $$ = append_declarator( NULL, $1 ); }
1047 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
1050 m_bitfield: { $$ = NULL; }
1051 | ':' expr_const { $$ = $2; }
1054 struct_declarator: any_declarator m_bitfield { $$ = $1; $$->bits = $2;
1055 if (!$$->bits && !$$->var->name)
1056 error_loc("unnamed fields are not allowed\n");
1060 struct_declarator_list:
1061 struct_declarator { $$ = append_declarator( NULL, $1 ); }
1062 | struct_declarator_list ',' struct_declarator
1063 { $$ = append_declarator( $1, $3 ); }
1066 init_declarator:
1067 declarator { $$ = $1; }
1068 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
1071 pointer_type:
1072 tREF { $$ = RPC_FC_RP; }
1073 | tUNIQUE { $$ = RPC_FC_UP; }
1074 | tPTR { $$ = RPC_FC_FP; }
1077 structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, TRUE, $4); }
1080 type: tVOID { $$ = type_new_void(); }
1081 | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
1082 | base_type { $$ = $1; }
1083 | enumdef { $$ = $1; }
1084 | tENUM aIDENTIFIER { $$ = type_new_enum($2, FALSE, NULL); }
1085 | structdef { $$ = $1; }
1086 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, FALSE, NULL); }
1087 | uniondef { $$ = $1; }
1088 | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, FALSE, NULL); }
1089 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
1092 typedef: tTYPEDEF m_attributes decl_spec declarator_list
1093 { reg_typedefs($3, $4, check_typedef_attrs($2));
1094 $$ = make_statement_typedef($4);
1098 uniondef: tUNION t_ident '{' ne_union_fields '}'
1099 { $$ = type_new_nonencapsulated_union($2, TRUE, $4); }
1100 | tUNION t_ident
1101 tSWITCH '(' s_field ')'
1102 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1105 version:
1106 aNUM { $$ = MAKEVERSION($1, 0); }
1107 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1112 static void decl_builtin_basic(const char *name, enum type_basic_type type)
1114 type_t *t = type_new_basic(type);
1115 reg_type(t, name, 0);
1118 static void decl_builtin_alias(const char *name, type_t *t)
1120 reg_type(type_new_alias(t, name), name, 0);
1123 void init_types(void)
1125 decl_builtin_basic("byte", TYPE_BASIC_BYTE);
1126 decl_builtin_basic("wchar_t", TYPE_BASIC_WCHAR);
1127 decl_builtin_basic("float", TYPE_BASIC_FLOAT);
1128 decl_builtin_basic("double", TYPE_BASIC_DOUBLE);
1129 decl_builtin_basic("error_status_t", TYPE_BASIC_ERROR_STATUS_T);
1130 decl_builtin_basic("handle_t", TYPE_BASIC_HANDLE);
1131 decl_builtin_alias("boolean", type_new_basic(TYPE_BASIC_BYTE));
1134 static str_list_t *append_str(str_list_t *list, char *str)
1136 struct str_list_entry_t *entry;
1138 if (!str) return list;
1139 if (!list)
1141 list = xmalloc( sizeof(*list) );
1142 list_init( list );
1144 entry = xmalloc( sizeof(*entry) );
1145 entry->str = str;
1146 list_add_tail( list, &entry->entry );
1147 return list;
1150 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1152 attr_t *attr_existing;
1153 if (!attr) return list;
1154 if (!list)
1156 list = xmalloc( sizeof(*list) );
1157 list_init( list );
1159 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1160 if (attr_existing->type == attr->type)
1162 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1163 /* use the last attribute, like MIDL does */
1164 list_remove(&attr_existing->entry);
1165 break;
1167 list_add_tail( list, &attr->entry );
1168 return list;
1171 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1173 attr_t *attr;
1174 if (!src) return dst;
1175 LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1176 if (attr->type == type)
1178 list_remove(&attr->entry);
1179 return append_attr(dst, attr);
1181 return dst;
1184 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1186 struct list *entry;
1188 if (!old_list) return new_list;
1190 while ((entry = list_head(old_list)))
1192 attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1193 list_remove(entry);
1194 new_list = append_attr(new_list, attr);
1196 return new_list;
1199 static attr_list_t *dupattrs(const attr_list_t *list)
1201 attr_list_t *new_list;
1202 const attr_t *attr;
1204 if (!list) return NULL;
1206 new_list = xmalloc( sizeof(*list) );
1207 list_init( new_list );
1208 LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1210 attr_t *new_attr = xmalloc(sizeof(*new_attr));
1211 *new_attr = *attr;
1212 list_add_tail(new_list, &new_attr->entry);
1214 return new_list;
1217 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass)
1219 decl_spec_t *declspec = left ? left : right;
1220 if (!declspec)
1222 declspec = xmalloc(sizeof(*declspec));
1223 declspec->type = NULL;
1224 declspec->attrs = NULL;
1225 declspec->stgclass = STG_NONE;
1227 declspec->type = type;
1228 if (left && declspec != left)
1230 declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1231 if (declspec->stgclass == STG_NONE)
1232 declspec->stgclass = left->stgclass;
1233 else if (left->stgclass != STG_NONE)
1234 error_loc("only one storage class can be specified\n");
1235 assert(!left->type);
1236 free(left);
1238 if (right && declspec != right)
1240 declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1241 if (declspec->stgclass == STG_NONE)
1242 declspec->stgclass = right->stgclass;
1243 else if (right->stgclass != STG_NONE)
1244 error_loc("only one storage class can be specified\n");
1245 assert(!right->type);
1246 free(right);
1249 declspec->attrs = append_attr(declspec->attrs, attr);
1250 if (declspec->stgclass == STG_NONE)
1251 declspec->stgclass = stgclass;
1252 else if (stgclass != STG_NONE)
1253 error_loc("only one storage class can be specified\n");
1255 /* apply attributes to type */
1256 if (type && declspec->attrs)
1258 attr_list_t *attrs;
1259 declspec->type = duptype(type, 1);
1260 attrs = dupattrs(type->attrs);
1261 declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1262 declspec->attrs = NULL;
1265 return declspec;
1268 static attr_t *make_attr(enum attr_type type)
1270 attr_t *a = xmalloc(sizeof(attr_t));
1271 a->type = type;
1272 a->u.ival = 0;
1273 return a;
1276 static attr_t *make_attrv(enum attr_type type, unsigned int val)
1278 attr_t *a = xmalloc(sizeof(attr_t));
1279 a->type = type;
1280 a->u.ival = val;
1281 return a;
1284 static attr_t *make_attrp(enum attr_type type, void *val)
1286 attr_t *a = xmalloc(sizeof(attr_t));
1287 a->type = type;
1288 a->u.pval = val;
1289 return a;
1292 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1294 if (!expr) return list;
1295 if (!list)
1297 list = xmalloc( sizeof(*list) );
1298 list_init( list );
1300 list_add_tail( list, &expr->entry );
1301 return list;
1304 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1306 if (!expr) return list;
1307 if (!list)
1309 list = xmalloc( sizeof(*list) );
1310 list_init( list );
1312 list_add_tail( list, &expr->entry );
1313 return list;
1316 static struct list type_pool = LIST_INIT(type_pool);
1317 typedef struct
1319 type_t data;
1320 struct list link;
1321 } type_pool_node_t;
1323 type_t *alloc_type(void)
1325 type_pool_node_t *node = xmalloc(sizeof *node);
1326 list_add_tail(&type_pool, &node->link);
1327 return &node->data;
1330 void set_all_tfswrite(int val)
1332 type_pool_node_t *node;
1333 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1334 node->data.tfswrite = val;
1337 void clear_all_offsets(void)
1339 type_pool_node_t *node;
1340 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1341 node->data.typestring_offset = node->data.ptrdesc = 0;
1344 static void type_function_add_head_arg(type_t *type, var_t *arg)
1346 if (!type->details.function->args)
1348 type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
1349 list_init( type->details.function->args );
1351 list_add_head( type->details.function->args, &arg->entry );
1354 static int is_allowed_range_type(const type_t *type)
1356 switch (type_get_type(type))
1358 case TYPE_ENUM:
1359 return TRUE;
1360 case TYPE_BASIC:
1361 switch (type_basic_get_type(type))
1363 case TYPE_BASIC_INT8:
1364 case TYPE_BASIC_INT16:
1365 case TYPE_BASIC_INT32:
1366 case TYPE_BASIC_INT64:
1367 case TYPE_BASIC_INT:
1368 case TYPE_BASIC_INT3264:
1369 case TYPE_BASIC_BYTE:
1370 case TYPE_BASIC_CHAR:
1371 case TYPE_BASIC_WCHAR:
1372 case TYPE_BASIC_HYPER:
1373 return TRUE;
1374 case TYPE_BASIC_FLOAT:
1375 case TYPE_BASIC_DOUBLE:
1376 case TYPE_BASIC_ERROR_STATUS_T:
1377 case TYPE_BASIC_HANDLE:
1378 return FALSE;
1380 return FALSE;
1381 default:
1382 return FALSE;
1386 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1388 type_t *ptrchain_type;
1389 if (!ptrchain)
1390 return type;
1391 for (ptrchain_type = ptrchain; type_pointer_get_ref(ptrchain_type); ptrchain_type = type_pointer_get_ref(ptrchain_type))
1393 assert(ptrchain_type->type_type == TYPE_POINTER);
1394 ptrchain_type->details.pointer.ref = type;
1395 return ptrchain;
1398 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl,
1399 int top)
1401 var_t *v = decl->var;
1402 expr_list_t *sizes = get_attrp(attrs, ATTR_SIZEIS);
1403 expr_list_t *lengs = get_attrp(attrs, ATTR_LENGTHIS);
1404 int sizeless;
1405 expr_t *dim;
1406 type_t **ptype;
1407 array_dims_t *arr = decl ? decl->array : NULL;
1408 type_t *func_type = decl ? decl->func_type : NULL;
1409 type_t *type = decl_spec->type;
1411 if (is_attr(type->attrs, ATTR_INLINE))
1413 if (!func_type)
1414 error_loc("inline attribute applied to non-function type\n");
1415 else
1417 type_t *t;
1418 /* move inline attribute from return type node to function node */
1419 for (t = func_type; is_ptr(t); t = type_pointer_get_ref(t))
1421 t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1425 /* add type onto the end of the pointers in pident->type */
1426 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1427 v->stgclass = decl_spec->stgclass;
1428 v->attrs = attrs;
1430 /* check for pointer attribute being applied to non-pointer, non-array
1431 * type */
1432 if (!arr)
1434 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1435 const type_t *ptr = NULL;
1436 /* pointer attributes on the left side of the type belong to the function
1437 * pointer, if one is being declared */
1438 type_t **pt = func_type ? &func_type : &v->type;
1439 for (ptr = *pt; ptr && !ptr_attr; )
1441 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1442 if (!ptr_attr && type_is_alias(ptr))
1443 ptr = type_alias_get_aliasee(ptr);
1444 else
1445 break;
1447 if (is_ptr(ptr))
1449 if (ptr_attr && ptr_attr != RPC_FC_UP &&
1450 type_get_type(type_pointer_get_ref(ptr)) == TYPE_INTERFACE)
1451 warning_loc_info(&v->loc_info,
1452 "%s: pointer attribute applied to interface "
1453 "pointer type has no effect\n", v->name);
1454 if (!ptr_attr && top && (*pt)->details.pointer.def_fc != RPC_FC_RP)
1456 /* FIXME: this is a horrible hack to cope with the issue that we
1457 * store an offset to the typeformat string in the type object, but
1458 * two typeformat strings may be written depending on whether the
1459 * pointer is a toplevel parameter or not */
1460 *pt = duptype(*pt, 1);
1463 else if (ptr_attr)
1464 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1467 if (is_attr(v->attrs, ATTR_STRING))
1469 type_t *t = type;
1471 if (!is_ptr(v->type) && !arr)
1472 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1473 v->name);
1475 while (is_ptr(t))
1476 t = type_pointer_get_ref(t);
1478 if (type_get_type(t) != TYPE_BASIC &&
1479 (get_basic_fc(t) != RPC_FC_CHAR &&
1480 get_basic_fc(t) != RPC_FC_BYTE &&
1481 get_basic_fc(t) != RPC_FC_WCHAR))
1483 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1484 v->name);
1488 if (is_attr(v->attrs, ATTR_V1ENUM))
1490 if (type_get_type_detect_alias(v->type) != TYPE_ENUM)
1491 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1494 if (is_attr(v->attrs, ATTR_RANGE) && !is_allowed_range_type(v->type))
1495 error_loc("'%s': [range] attribute applied to non-integer type\n",
1496 v->name);
1498 ptype = &v->type;
1499 sizeless = FALSE;
1500 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1502 if (sizeless)
1503 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1505 if (dim->is_const)
1507 if (dim->cval <= 0)
1508 error_loc("%s: array dimension must be positive\n", v->name);
1510 /* FIXME: should use a type_memsize that allows us to pass in a pointer size */
1511 if (0)
1513 unsigned int size = type_memsize(v->type);
1515 if (0xffffffffu / size < dim->cval)
1516 error_loc("%s: total array size is too large\n", v->name);
1519 else
1520 sizeless = TRUE;
1522 *ptype = type_new_array(NULL, *ptype, FALSE,
1523 dim->is_const ? dim->cval : 0,
1524 dim->is_const ? NULL : dim, NULL,
1525 pointer_default);
1528 ptype = &v->type;
1529 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1531 if (dim->type != EXPR_VOID)
1533 if (is_array(*ptype))
1535 if (!type_array_get_conformance(*ptype) ||
1536 type_array_get_conformance(*ptype)->type != EXPR_VOID)
1537 error_loc("%s: cannot specify size_is for an already sized array\n", v->name);
1538 else
1539 *ptype = type_new_array((*ptype)->name,
1540 type_array_get_element(*ptype), FALSE,
1541 0, dim, NULL, 0);
1543 else if (is_ptr(*ptype))
1544 *ptype = type_new_array((*ptype)->name, type_pointer_get_ref(*ptype), TRUE,
1545 0, dim, NULL, pointer_default);
1546 else
1547 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1550 if (is_ptr(*ptype))
1551 ptype = &(*ptype)->details.pointer.ref;
1552 else if (is_array(*ptype))
1553 ptype = &(*ptype)->details.array.elem;
1554 else
1555 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1558 ptype = &v->type;
1559 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1561 if (dim->type != EXPR_VOID)
1563 if (is_array(*ptype))
1565 *ptype = type_new_array((*ptype)->name,
1566 type_array_get_element(*ptype),
1567 type_array_is_decl_as_ptr(*ptype),
1568 type_array_get_dim(*ptype),
1569 type_array_get_conformance(*ptype),
1570 dim, type_array_get_ptr_default_fc(*ptype));
1572 else
1573 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1576 if (is_ptr(*ptype))
1577 ptype = &(*ptype)->details.pointer.ref;
1578 else if (is_array(*ptype))
1579 ptype = &(*ptype)->details.array.elem;
1580 else
1581 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1584 /* v->type is currently pointing to the type on the left-side of the
1585 * declaration, so we need to fix this up so that it is the return type of the
1586 * function and make v->type point to the function side of the declaration */
1587 if (func_type)
1589 type_t *ft, *t;
1590 type_t *return_type = v->type;
1591 v->type = func_type;
1592 for (ft = v->type; is_ptr(ft); ft = type_pointer_get_ref(ft))
1594 assert(type_get_type_detect_alias(ft) == TYPE_FUNCTION);
1595 ft->details.function->rettype = return_type;
1596 /* move calling convention attribute, if present, from pointer nodes to
1597 * function node */
1598 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1599 ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1601 else
1603 type_t *t;
1604 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1605 if (is_attr(t->attrs, ATTR_CALLCONV))
1606 error_loc("calling convention applied to non-function-pointer type\n");
1609 if (decl->bits)
1610 v->type = type_new_bitfield(v->type, decl->bits);
1612 return v;
1615 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1617 declarator_t *decl, *next;
1618 var_list_t *var_list = NULL;
1620 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1622 var_t *var = declare_var(attrs, decl_spec, decl, 0);
1623 var_list = append_var(var_list, var);
1624 free(decl);
1626 free(decl_spec);
1627 return var_list;
1630 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1632 if (!iface) return list;
1633 if (!list)
1635 list = xmalloc( sizeof(*list) );
1636 list_init( list );
1638 list_add_tail( list, &iface->entry );
1639 return list;
1642 static ifref_t *make_ifref(type_t *iface)
1644 ifref_t *l = xmalloc(sizeof(ifref_t));
1645 l->iface = iface;
1646 l->attrs = NULL;
1647 return l;
1650 var_list_t *append_var(var_list_t *list, var_t *var)
1652 if (!var) return list;
1653 if (!list)
1655 list = xmalloc( sizeof(*list) );
1656 list_init( list );
1658 list_add_tail( list, &var->entry );
1659 return list;
1662 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1664 if (!vars) return list;
1665 if (!list)
1667 list = xmalloc( sizeof(*list) );
1668 list_init( list );
1670 list_move_tail( list, vars );
1671 return list;
1674 var_t *make_var(char *name)
1676 var_t *v = xmalloc(sizeof(var_t));
1677 v->name = name;
1678 v->type = NULL;
1679 v->attrs = NULL;
1680 v->eval = NULL;
1681 v->stgclass = STG_NONE;
1682 init_loc_info(&v->loc_info);
1683 return v;
1686 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1688 if (!d) return list;
1689 if (!list) {
1690 list = xmalloc(sizeof(*list));
1691 list_init(list);
1693 list_add_tail(list, &d->entry);
1694 return list;
1697 static declarator_t *make_declarator(var_t *var)
1699 declarator_t *d = xmalloc(sizeof(*d));
1700 d->var = var ? var : make_var(NULL);
1701 d->type = NULL;
1702 d->func_type = NULL;
1703 d->array = NULL;
1704 d->bits = NULL;
1705 return d;
1708 static type_t *make_safearray(type_t *type)
1710 return type_new_array(NULL, type_new_alias(type, "SAFEARRAY"), TRUE, 0,
1711 NULL, NULL, RPC_FC_RP);
1714 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1716 typelib_t *typelib = xmalloc(sizeof(*typelib));
1717 typelib->name = xstrdup(name);
1718 typelib->filename = NULL;
1719 typelib->attrs = attrs;
1720 list_init( &typelib->importlibs );
1721 return typelib;
1724 #define HASHMAX 64
1726 static int hash_ident(const char *name)
1728 const char *p = name;
1729 int sum = 0;
1730 /* a simple sum hash is probably good enough */
1731 while (*p) {
1732 sum += *p;
1733 p++;
1735 return sum & (HASHMAX-1);
1738 /***** type repository *****/
1740 struct rtype {
1741 const char *name;
1742 type_t *type;
1743 int t;
1744 struct rtype *next;
1747 struct rtype *type_hash[HASHMAX];
1749 type_t *reg_type(type_t *type, const char *name, int t)
1751 struct rtype *nt;
1752 int hash;
1753 if (!name) {
1754 error_loc("registering named type without name\n");
1755 return type;
1757 hash = hash_ident(name);
1758 nt = xmalloc(sizeof(struct rtype));
1759 nt->name = name;
1760 nt->type = type;
1761 nt->t = t;
1762 nt->next = type_hash[hash];
1763 type_hash[hash] = nt;
1764 if ((t == tsSTRUCT || t == tsUNION))
1765 fix_incomplete_types(type);
1766 return type;
1769 static int is_incomplete(const type_t *t)
1771 return !t->defined &&
1772 (type_get_type_detect_alias(t) == TYPE_STRUCT ||
1773 type_get_type_detect_alias(t) == TYPE_UNION ||
1774 type_get_type_detect_alias(t) == TYPE_ENCAPSULATED_UNION);
1777 void add_incomplete(type_t *t)
1779 struct typenode *tn = xmalloc(sizeof *tn);
1780 tn->type = t;
1781 list_add_tail(&incomplete_types, &tn->entry);
1784 static void fix_type(type_t *t)
1786 if (type_is_alias(t) && is_incomplete(t)) {
1787 type_t *ot = type_alias_get_aliasee(t);
1788 fix_type(ot);
1789 if (type_get_type_detect_alias(ot) == TYPE_STRUCT ||
1790 type_get_type_detect_alias(ot) == TYPE_UNION ||
1791 type_get_type_detect_alias(ot) == TYPE_ENCAPSULATED_UNION)
1792 t->details.structure = ot->details.structure;
1793 t->defined = ot->defined;
1797 static void fix_incomplete(void)
1799 struct typenode *tn, *next;
1801 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1802 fix_type(tn->type);
1803 list_remove(&tn->entry);
1804 free(tn);
1808 static void fix_incomplete_types(type_t *complete_type)
1810 struct typenode *tn, *next;
1812 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry)
1814 if (type_is_equal(complete_type, tn->type))
1816 tn->type->details.structure = complete_type->details.structure;
1817 list_remove(&tn->entry);
1818 free(tn);
1823 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1825 const declarator_t *decl;
1826 type_t *type = decl_spec->type;
1828 /* We must generate names for tagless enum, struct or union.
1829 Typedef-ing a tagless enum, struct or union means we want the typedef
1830 to be included in a library hence the public attribute. */
1831 if ((type_get_type_detect_alias(type) == TYPE_ENUM ||
1832 type_get_type_detect_alias(type) == TYPE_STRUCT ||
1833 type_get_type_detect_alias(type) == TYPE_UNION ||
1834 type_get_type_detect_alias(type) == TYPE_ENCAPSULATED_UNION) &&
1835 !type->name && !parse_only)
1837 if (! is_attr(attrs, ATTR_PUBLIC))
1838 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1839 type->name = gen_name();
1841 else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1842 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1844 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1847 if (decl->var->name) {
1848 type_t *cur;
1849 var_t *name;
1851 cur = find_type(decl->var->name, 0);
1852 if (cur)
1853 error_loc("%s: redefinition error; original definition was at %s:%d\n",
1854 cur->name, cur->loc_info.input_name,
1855 cur->loc_info.line_number);
1857 name = declare_var(attrs, decl_spec, decl, 0);
1858 cur = type_new_alias(name->type, name->name);
1859 cur->attrs = attrs;
1861 if (is_incomplete(cur))
1862 add_incomplete(cur);
1863 reg_type(cur, cur->name, 0);
1866 return type;
1869 type_t *find_type(const char *name, int t)
1871 struct rtype *cur = type_hash[hash_ident(name)];
1872 while (cur && (cur->t != t || strcmp(cur->name, name)))
1873 cur = cur->next;
1874 return cur ? cur->type : NULL;
1877 static type_t *find_type_or_error(const char *name, int t)
1879 type_t *type = find_type(name, t);
1880 if (!type) {
1881 error_loc("type '%s' not found\n", name);
1882 return NULL;
1884 return type;
1887 static type_t *find_type_or_error2(char *name, int t)
1889 type_t *tp = find_type_or_error(name, t);
1890 free(name);
1891 return tp;
1894 int is_type(const char *name)
1896 return find_type(name, 0) != NULL;
1899 type_t *get_type(enum type_type type, char *name, int t)
1901 type_t *tp;
1902 if (name) {
1903 tp = find_type(name, t);
1904 if (tp) {
1905 free(name);
1906 return tp;
1909 tp = make_type(type);
1910 tp->name = name;
1911 if (!name) return tp;
1912 return reg_type(tp, name, t);
1915 /***** constant repository *****/
1917 struct rconst {
1918 char *name;
1919 var_t *var;
1920 struct rconst *next;
1923 struct rconst *const_hash[HASHMAX];
1925 static var_t *reg_const(var_t *var)
1927 struct rconst *nc;
1928 int hash;
1929 if (!var->name) {
1930 error_loc("registering constant without name\n");
1931 return var;
1933 hash = hash_ident(var->name);
1934 nc = xmalloc(sizeof(struct rconst));
1935 nc->name = var->name;
1936 nc->var = var;
1937 nc->next = const_hash[hash];
1938 const_hash[hash] = nc;
1939 return var;
1942 var_t *find_const(const char *name, int f)
1944 struct rconst *cur = const_hash[hash_ident(name)];
1945 while (cur && strcmp(cur->name, name))
1946 cur = cur->next;
1947 if (!cur) {
1948 if (f) error_loc("constant '%s' not found\n", name);
1949 return NULL;
1951 return cur->var;
1954 static char *gen_name(void)
1956 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1957 static unsigned long n = 0;
1958 static const char *file_id;
1959 static size_t size;
1960 char *name;
1962 if (! file_id)
1964 char *dst = dup_basename(input_name, ".idl");
1965 file_id = dst;
1967 for (; *dst; ++dst)
1968 if (! isalnum((unsigned char) *dst))
1969 *dst = '_';
1971 size = sizeof format - 7 + strlen(file_id) + 8;
1974 name = xmalloc(size);
1975 sprintf(name, format, file_id, n++);
1976 return name;
1979 struct allowed_attr
1981 unsigned int dce_compatible : 1;
1982 unsigned int acf : 1;
1983 unsigned int on_interface : 1;
1984 unsigned int on_function : 1;
1985 unsigned int on_arg : 1;
1986 unsigned int on_type : 1;
1987 unsigned int on_enum : 1;
1988 unsigned int on_struct : 1;
1989 unsigned int on_union : 1;
1990 unsigned int on_field : 1;
1991 unsigned int on_library : 1;
1992 unsigned int on_dispinterface : 1;
1993 unsigned int on_module : 1;
1994 unsigned int on_coclass : 1;
1995 const char *display_name;
1998 struct allowed_attr allowed_attr[] =
2000 /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
2001 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2002 /* ATTR_ANNOTATION */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "annotation" },
2003 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2004 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
2005 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2006 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2007 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2008 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2009 /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2010 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2011 /* ATTR_CODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "code" },
2012 /* ATTR_COMMSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "comm_status" },
2013 /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
2014 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
2015 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2016 /* ATTR_DECODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "decode" },
2017 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
2018 /* ATTR_DEFAULTBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultbind" },
2019 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2020 /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2021 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2022 /* ATTR_DISABLECONSISTENCYCHECK */{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "disable_consistency_check" },
2023 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2024 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2025 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2026 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2027 /* ATTR_ENABLEALLOCATE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "enable_allocate" },
2028 /* ATTR_ENCODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "encode" },
2029 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2030 /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2031 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2032 /* ATTR_FAULTSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "fault_status" },
2033 /* ATTR_FORCEALLOCATE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "force_allocate" },
2034 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2035 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2036 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2037 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2038 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2039 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2040 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2041 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
2042 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2043 /* ATTR_IGNORE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "ignore" },
2044 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2045 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2046 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2047 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2048 /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2049 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2050 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2051 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2052 /* ATTR_LICENSED */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "licensed" },
2053 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2054 /* ATTR_MAYBE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "maybe" },
2055 /* ATTR_MESSAGE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "message" },
2056 /* ATTR_NOCODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nocode" },
2057 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2058 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2059 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2060 /* ATTR_NOTIFY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify" },
2061 /* ATTR_NOTIFYFLAG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify_flag" },
2062 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2063 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2064 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2065 /* ATTR_OPTIMIZE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optimize" },
2066 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2067 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2068 /* ATTR_PARAMLCID */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "lcid" },
2069 /* ATTR_PARTIALIGNORE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "partial_ignore" },
2070 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2071 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2072 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2073 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2074 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2075 /* ATTR_PROXY */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "proxy" },
2076 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2077 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2078 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2079 /* ATTR_REPRESENTAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "represent_as" },
2080 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2081 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2082 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2083 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2084 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2085 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2086 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2087 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2088 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2089 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2090 /* ATTR_UIDEFAULT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "uidefault" },
2091 /* ATTR_USESGETLASTERROR */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "usesgetlasterror" },
2092 /* ATTR_USERMARSHAL */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "user_marshal" },
2093 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
2094 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2095 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2096 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2097 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2100 const char *get_attr_display_name(enum attr_type type)
2102 return allowed_attr[type].display_name;
2105 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2107 const attr_t *attr;
2108 if (!attrs) return attrs;
2109 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2111 if (!allowed_attr[attr->type].on_interface)
2112 error_loc("inapplicable attribute %s for interface %s\n",
2113 allowed_attr[attr->type].display_name, name);
2115 return attrs;
2118 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2120 const attr_t *attr;
2121 if (!attrs) return attrs;
2122 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2124 if (!allowed_attr[attr->type].on_function)
2125 error_loc("inapplicable attribute %s for function %s\n",
2126 allowed_attr[attr->type].display_name, name);
2128 return attrs;
2131 static void check_arg_attrs(const var_t *arg)
2133 const attr_t *attr;
2135 if (arg->attrs)
2137 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2139 if (!allowed_attr[attr->type].on_arg)
2140 error_loc("inapplicable attribute %s for argument %s\n",
2141 allowed_attr[attr->type].display_name, arg->name);
2146 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2148 const attr_t *attr;
2149 if (!attrs) return attrs;
2150 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2152 if (!allowed_attr[attr->type].on_type)
2153 error_loc("inapplicable attribute %s for typedef\n",
2154 allowed_attr[attr->type].display_name);
2156 return attrs;
2159 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2161 const attr_t *attr;
2162 if (!attrs) return attrs;
2163 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2165 if (!allowed_attr[attr->type].on_enum)
2166 error_loc("inapplicable attribute %s for enum\n",
2167 allowed_attr[attr->type].display_name);
2169 return attrs;
2172 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2174 const attr_t *attr;
2175 if (!attrs) return attrs;
2176 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2178 if (!allowed_attr[attr->type].on_struct)
2179 error_loc("inapplicable attribute %s for struct\n",
2180 allowed_attr[attr->type].display_name);
2182 return attrs;
2185 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2187 const attr_t *attr;
2188 if (!attrs) return attrs;
2189 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2191 if (!allowed_attr[attr->type].on_union)
2192 error_loc("inapplicable attribute %s for union\n",
2193 allowed_attr[attr->type].display_name);
2195 return attrs;
2198 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2200 const attr_t *attr;
2201 if (!attrs) return attrs;
2202 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2204 if (!allowed_attr[attr->type].on_field)
2205 error_loc("inapplicable attribute %s for field %s\n",
2206 allowed_attr[attr->type].display_name, name);
2208 return attrs;
2211 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2213 const attr_t *attr;
2214 if (!attrs) return attrs;
2215 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2217 if (!allowed_attr[attr->type].on_library)
2218 error_loc("inapplicable attribute %s for library %s\n",
2219 allowed_attr[attr->type].display_name, name);
2221 return attrs;
2224 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2226 const attr_t *attr;
2227 if (!attrs) return attrs;
2228 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2230 if (!allowed_attr[attr->type].on_dispinterface)
2231 error_loc("inapplicable attribute %s for dispinterface %s\n",
2232 allowed_attr[attr->type].display_name, name);
2234 return attrs;
2237 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2239 const attr_t *attr;
2240 if (!attrs) return attrs;
2241 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2243 if (!allowed_attr[attr->type].on_module)
2244 error_loc("inapplicable attribute %s for module %s\n",
2245 allowed_attr[attr->type].display_name, name);
2247 return attrs;
2250 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2252 const attr_t *attr;
2253 if (!attrs) return attrs;
2254 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2256 if (!allowed_attr[attr->type].on_coclass)
2257 error_loc("inapplicable attribute %s for coclass %s\n",
2258 allowed_attr[attr->type].display_name, name);
2260 return attrs;
2263 static int is_allowed_conf_type(const type_t *type)
2265 switch (type_get_type(type))
2267 case TYPE_ENUM:
2268 return TRUE;
2269 case TYPE_BASIC:
2270 switch (type_basic_get_type(type))
2272 case TYPE_BASIC_INT8:
2273 case TYPE_BASIC_INT16:
2274 case TYPE_BASIC_INT32:
2275 case TYPE_BASIC_INT64:
2276 case TYPE_BASIC_INT:
2277 case TYPE_BASIC_CHAR:
2278 case TYPE_BASIC_HYPER:
2279 case TYPE_BASIC_BYTE:
2280 case TYPE_BASIC_WCHAR:
2281 return TRUE;
2282 default:
2283 return FALSE;
2285 case TYPE_ALIAS:
2286 /* shouldn't get here because of type_get_type call above */
2287 assert(0);
2288 /* fall through */
2289 case TYPE_STRUCT:
2290 case TYPE_UNION:
2291 case TYPE_ENCAPSULATED_UNION:
2292 case TYPE_ARRAY:
2293 case TYPE_POINTER:
2294 case TYPE_VOID:
2295 case TYPE_MODULE:
2296 case TYPE_COCLASS:
2297 case TYPE_FUNCTION:
2298 case TYPE_INTERFACE:
2299 case TYPE_BITFIELD:
2300 return FALSE;
2302 return FALSE;
2305 static int is_ptr_guid_type(const type_t *type)
2307 /* first, make sure it is a pointer to something */
2308 if (!is_ptr(type)) return FALSE;
2310 /* second, make sure it is a pointer to something of size sizeof(GUID),
2311 * i.e. 16 bytes */
2312 return (type_memsize(type_pointer_get_ref(type)) == 16);
2315 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2317 expr_t *dim;
2318 struct expr_loc expr_loc;
2319 expr_loc.v = arg;
2320 expr_loc.attr = attr_name;
2321 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2323 if (dim->type != EXPR_VOID)
2325 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2326 if (!is_allowed_conf_type(expr_type))
2327 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2328 attr_name);
2333 static void check_remoting_fields(const var_t *var, type_t *type);
2335 /* checks that properties common to fields and arguments are consistent */
2336 static void check_field_common(const type_t *container_type,
2337 const char *container_name, const var_t *arg)
2339 type_t *type = arg->type;
2340 int more_to_do;
2341 const char *container_type_name;
2342 const char *var_type;
2344 switch (type_get_type(container_type))
2346 case TYPE_STRUCT:
2347 container_type_name = "struct";
2348 var_type = "field";
2349 break;
2350 case TYPE_UNION:
2351 container_type_name = "union";
2352 var_type = "arm";
2353 break;
2354 case TYPE_ENCAPSULATED_UNION:
2355 container_type_name = "encapsulated union";
2356 var_type = "arm";
2357 break;
2358 case TYPE_FUNCTION:
2359 container_type_name = "function";
2360 var_type = "parameter";
2361 break;
2362 default:
2363 /* should be no other container types */
2364 assert(0);
2365 return;
2368 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2369 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2370 error_loc_info(&arg->loc_info,
2371 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2372 arg->name);
2374 if (is_attr(arg->attrs, ATTR_SIZEIS))
2376 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2377 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2379 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2381 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2382 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2384 if (is_attr(arg->attrs, ATTR_IIDIS))
2386 struct expr_loc expr_loc;
2387 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2388 if (expr->type != EXPR_VOID)
2390 const type_t *expr_type;
2391 expr_loc.v = arg;
2392 expr_loc.attr = "iid_is";
2393 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2394 if (!expr_type || !is_ptr_guid_type(expr_type))
2395 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2398 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2400 struct expr_loc expr_loc;
2401 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2402 if (expr->type != EXPR_VOID)
2404 const type_t *expr_type;
2405 expr_loc.v = arg;
2406 expr_loc.attr = "switch_is";
2407 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2408 if (!expr_type || !is_allowed_conf_type(expr_type))
2409 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2410 expr_loc.attr);
2416 more_to_do = FALSE;
2418 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
2420 case TGT_STRUCT:
2421 case TGT_UNION:
2422 check_remoting_fields(arg, type);
2423 break;
2424 case TGT_INVALID:
2426 const char *reason = "is invalid";
2427 switch (type_get_type(type))
2429 case TYPE_VOID:
2430 reason = "cannot derive from void *";
2431 break;
2432 case TYPE_FUNCTION:
2433 reason = "cannot be a function pointer";
2434 break;
2435 case TYPE_BITFIELD:
2436 reason = "cannot be a bit-field";
2437 break;
2438 case TYPE_COCLASS:
2439 reason = "cannot be a class";
2440 break;
2441 case TYPE_INTERFACE:
2442 reason = "cannot be a non-pointer to an interface";
2443 break;
2444 case TYPE_MODULE:
2445 reason = "cannot be a module";
2446 break;
2447 default:
2448 break;
2450 error_loc_info(&arg->loc_info, "%s \'%s\' of %s \'%s\' %s\n",
2451 var_type, arg->name, container_type_name, container_name, reason);
2452 break;
2454 case TGT_CTXT_HANDLE:
2455 case TGT_CTXT_HANDLE_POINTER:
2456 if (type_get_type(container_type) != TYPE_FUNCTION)
2457 error_loc_info(&arg->loc_info,
2458 "%s \'%s\' of %s \'%s\' cannot be a context handle\n",
2459 var_type, arg->name, container_type_name,
2460 container_name);
2461 break;
2462 case TGT_STRING:
2464 const type_t *t = type;
2465 while (is_ptr(t))
2466 t = type_pointer_get_ref(t);
2467 if (is_aliaschain_attr(t, ATTR_RANGE))
2468 warning_loc_info(&arg->loc_info, "%s: range not verified for a string of ranged types\n", arg->name);
2469 break;
2471 case TGT_POINTER:
2472 type = type_pointer_get_ref(type);
2473 more_to_do = TRUE;
2474 break;
2475 case TGT_ARRAY:
2476 type = type_array_get_element(type);
2477 more_to_do = TRUE;
2478 break;
2479 case TGT_USER_TYPE:
2480 case TGT_IFACE_POINTER:
2481 case TGT_BASIC:
2482 case TGT_ENUM:
2483 case TGT_RANGE:
2484 /* nothing to do */
2485 break;
2487 } while (more_to_do);
2490 static void check_remoting_fields(const var_t *var, type_t *type)
2492 const var_t *field;
2493 const var_list_t *fields = NULL;
2495 type = type_get_real_type(type);
2497 if (type->checked)
2498 return;
2500 type->checked = TRUE;
2502 if (type_get_type(type) == TYPE_STRUCT)
2504 if (type_is_complete(type))
2505 fields = type_struct_get_fields(type);
2506 else
2507 error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2509 else if (type_get_type(type) == TYPE_UNION || type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2510 fields = type_union_get_cases(type);
2512 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2513 if (field->type) check_field_common(type, type->name, field);
2516 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2517 static void check_remoting_args(const var_t *func)
2519 const char *funcname = func->name;
2520 const var_t *arg;
2522 if (func->type->details.function->args) LIST_FOR_EACH_ENTRY( arg, func->type->details.function->args, const var_t, entry )
2524 const type_t *type = arg->type;
2526 /* check that [out] parameters have enough pointer levels */
2527 if (is_attr(arg->attrs, ATTR_OUT))
2529 switch (typegen_detect_type(type, arg->attrs, TDT_ALL_TYPES))
2531 case TGT_BASIC:
2532 case TGT_ENUM:
2533 case TGT_RANGE:
2534 case TGT_STRUCT:
2535 case TGT_UNION:
2536 case TGT_CTXT_HANDLE:
2537 case TGT_USER_TYPE:
2538 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2539 break;
2540 case TGT_IFACE_POINTER:
2541 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2542 break;
2543 case TGT_STRING:
2544 if (is_ptr(type) ||
2545 (is_array(type) &&
2546 (!type_array_has_conformance(type) ||
2547 type_array_get_conformance(type)->type == EXPR_VOID)))
2548 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' cannot be an unsized string\n", arg->name, funcname);
2549 break;
2550 case TGT_INVALID:
2551 /* already error'd before we get here */
2552 case TGT_CTXT_HANDLE_POINTER:
2553 case TGT_POINTER:
2554 case TGT_ARRAY:
2555 /* OK */
2556 break;
2560 check_field_common(func->type, funcname, arg);
2563 if (type_get_type(type_function_get_rettype(func->type)) != TYPE_VOID)
2565 var_t var;
2566 var = *func;
2567 var.type = type_function_get_rettype(func->type);
2568 var.name = xstrdup("return value");
2569 check_field_common(func->type, funcname, &var);
2570 free(var.name);
2574 static void add_explicit_handle_if_necessary(var_t *func)
2576 const var_t* explicit_handle_var;
2577 const var_t* explicit_generic_handle_var = NULL;
2578 const var_t* context_handle_var = NULL;
2580 /* check for a defined binding handle */
2581 explicit_handle_var = get_explicit_handle_var(func);
2582 if (!explicit_handle_var)
2584 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2585 if (!explicit_generic_handle_var)
2587 context_handle_var = get_context_handle_var(func);
2588 if (!context_handle_var)
2590 /* no explicit handle specified so add
2591 * "[in] handle_t IDL_handle" as the first parameter to the
2592 * function */
2593 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2594 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2595 idl_handle->type = find_type_or_error("handle_t", 0);
2596 type_function_add_head_arg(func->type, idl_handle);
2602 static void check_functions(const type_t *iface, int is_inside_library)
2604 const statement_t *stmt;
2605 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
2607 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2609 var_t *func = stmt->u.var;
2610 add_explicit_handle_if_necessary(func);
2613 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2615 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2617 const var_t *func = stmt->u.var;
2618 if (!is_attr(func->attrs, ATTR_LOCAL))
2619 check_remoting_args(func);
2624 static void check_statements(const statement_list_t *stmts, int is_inside_library)
2626 const statement_t *stmt;
2628 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2630 if (stmt->type == STMT_LIBRARY)
2631 check_statements(stmt->u.lib->stmts, TRUE);
2632 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
2633 check_functions(stmt->u.type, is_inside_library);
2637 static void check_all_user_types(const statement_list_t *stmts)
2639 const statement_t *stmt;
2641 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2643 if (stmt->type == STMT_LIBRARY)
2644 check_all_user_types(stmt->u.lib->stmts);
2645 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE &&
2646 !is_local(stmt->u.type->attrs))
2648 const statement_t *stmt_func;
2649 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type)) {
2650 const var_t *func = stmt_func->u.var;
2651 check_for_additional_prototype_types(func->type->details.function->args);
2657 int is_valid_uuid(const char *s)
2659 int i;
2661 for (i = 0; i < 36; ++i)
2662 if (i == 8 || i == 13 || i == 18 || i == 23)
2664 if (s[i] != '-')
2665 return FALSE;
2667 else
2668 if (!isxdigit(s[i]))
2669 return FALSE;
2671 return s[i] == '\0';
2674 static statement_t *make_statement(enum statement_type type)
2676 statement_t *stmt = xmalloc(sizeof(*stmt));
2677 stmt->type = type;
2678 return stmt;
2681 static statement_t *make_statement_type_decl(type_t *type)
2683 statement_t *stmt = make_statement(STMT_TYPE);
2684 stmt->u.type = type;
2685 return stmt;
2688 static statement_t *make_statement_reference(type_t *type)
2690 statement_t *stmt = make_statement(STMT_TYPEREF);
2691 stmt->u.type = type;
2692 return stmt;
2695 static statement_t *make_statement_declaration(var_t *var)
2697 statement_t *stmt = make_statement(STMT_DECLARATION);
2698 stmt->u.var = var;
2699 if (var->stgclass == STG_EXTERN && var->eval)
2700 warning("'%s' initialised and declared extern\n", var->name);
2701 if (is_const_decl(var))
2703 if (var->eval)
2704 reg_const(var);
2706 else if (type_get_type(var->type) == TYPE_FUNCTION)
2707 check_function_attrs(var->name, var->attrs);
2708 else if (var->stgclass == STG_NONE || var->stgclass == STG_REGISTER)
2709 error_loc("instantiation of data is illegal\n");
2710 return stmt;
2713 static statement_t *make_statement_library(typelib_t *typelib)
2715 statement_t *stmt = make_statement(STMT_LIBRARY);
2716 stmt->u.lib = typelib;
2717 return stmt;
2720 static statement_t *make_statement_cppquote(const char *str)
2722 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2723 stmt->u.str = str;
2724 return stmt;
2727 static statement_t *make_statement_importlib(const char *str)
2729 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2730 stmt->u.str = str;
2731 return stmt;
2734 static statement_t *make_statement_import(const char *str)
2736 statement_t *stmt = make_statement(STMT_IMPORT);
2737 stmt->u.str = str;
2738 return stmt;
2741 static statement_t *make_statement_module(type_t *type)
2743 statement_t *stmt = make_statement(STMT_MODULE);
2744 stmt->u.type = type;
2745 return stmt;
2748 static statement_t *make_statement_typedef(declarator_list_t *decls)
2750 declarator_t *decl, *next;
2751 statement_t *stmt;
2752 type_list_t **type_list;
2754 if (!decls) return NULL;
2756 stmt = make_statement(STMT_TYPEDEF);
2757 stmt->u.type_list = NULL;
2758 type_list = &stmt->u.type_list;
2760 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2762 var_t *var = decl->var;
2763 type_t *type = find_type_or_error(var->name, 0);
2764 *type_list = xmalloc(sizeof(type_list_t));
2765 (*type_list)->type = type;
2766 (*type_list)->next = NULL;
2768 type_list = &(*type_list)->next;
2769 free(decl);
2770 free(var);
2773 return stmt;
2776 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2778 if (!stmt) return list;
2779 if (!list)
2781 list = xmalloc( sizeof(*list) );
2782 list_init( list );
2784 list_add_tail( list, &stmt->entry );
2785 return list;
2788 void init_loc_info(loc_info_t *i)
2790 i->input_name = input_name ? input_name : "stdin";
2791 i->line_number = line_number;
2792 i->near_text = parser_text;
2795 static void check_def(const type_t *t)
2797 if (t->defined)
2798 error_loc("%s: redefinition error; original definition was at %s:%d\n",
2799 t->name, t->loc_info.input_name, t->loc_info.line_number);