winecfg: Constrain DPI values to the commonly supported ones.
[wine.git] / tools / widl / parser.y
blobc262ca8f1a425203a76ef5a1e320c25dbf305675
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 static unsigned char pointer_default = RPC_FC_UP;
43 typedef struct list typelist_t;
44 struct typenode {
45 type_t *type;
46 struct list entry;
49 struct _import_t
51 char *name;
52 int import_performed;
55 typedef struct _decl_spec_t
57 type_t *type;
58 attr_list_t *attrs;
59 enum storage_class stgclass;
60 } decl_spec_t;
62 typelist_t incomplete_types = LIST_INIT(incomplete_types);
64 static void fix_incomplete(void);
65 static void fix_incomplete_types(type_t *complete_type);
67 static str_list_t *append_str(str_list_t *list, char *str);
68 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
69 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
70 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass);
71 static attr_t *make_attr(enum attr_type type);
72 static attr_t *make_attrv(enum attr_type type, unsigned int val);
73 static attr_t *make_attrp(enum attr_type type, void *val);
74 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
75 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
76 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl, int top);
77 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
78 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
79 static ifref_t *make_ifref(type_t *iface);
80 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
81 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
82 static declarator_t *make_declarator(var_t *var);
83 static type_t *make_safearray(type_t *type);
84 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
85 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
86 static warning_list_t *append_warning(warning_list_t *, int);
88 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
89 static type_t *find_type_or_error(const char *name, int t);
90 static type_t *find_type_or_error2(char *name, int t);
92 static var_t *reg_const(var_t *var);
94 static void push_namespace(const char *name);
95 static void pop_namespace(const char *name);
97 static char *gen_name(void);
98 static void check_arg_attrs(const var_t *arg);
99 static void check_statements(const statement_list_t *stmts, int is_inside_library);
100 static void check_all_user_types(const statement_list_t *stmts);
101 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
102 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
103 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
104 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
105 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
106 static attr_list_t *check_union_attrs(attr_list_t *attrs);
107 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
108 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
109 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
110 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
111 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
112 const char *get_attr_display_name(enum attr_type type);
113 static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func);
114 static void check_def(const type_t *t);
116 static statement_t *make_statement(enum statement_type type);
117 static statement_t *make_statement_type_decl(type_t *type);
118 static statement_t *make_statement_reference(type_t *type);
119 static statement_t *make_statement_declaration(var_t *var);
120 static statement_t *make_statement_library(typelib_t *typelib);
121 static statement_t *make_statement_pragma(const char *str);
122 static statement_t *make_statement_cppquote(const char *str);
123 static statement_t *make_statement_importlib(const char *str);
124 static statement_t *make_statement_module(type_t *type);
125 static statement_t *make_statement_typedef(var_list_t *names);
126 static statement_t *make_statement_import(const char *str);
127 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
128 static statement_list_t *append_statements(statement_list_t *, statement_list_t *);
129 static attr_list_t *append_attribs(attr_list_t *, attr_list_t *);
131 static struct namespace global_namespace = {
132 NULL, NULL, LIST_INIT(global_namespace.entry), LIST_INIT(global_namespace.children)
135 static struct namespace *current_namespace = &global_namespace;
138 %union {
139 attr_t *attr;
140 attr_list_t *attr_list;
141 str_list_t *str_list;
142 expr_t *expr;
143 expr_list_t *expr_list;
144 array_dims_t *array_dims;
145 type_t *type;
146 var_t *var;
147 var_list_t *var_list;
148 declarator_t *declarator;
149 declarator_list_t *declarator_list;
150 statement_t *statement;
151 statement_list_t *stmt_list;
152 warning_t *warning;
153 warning_list_t *warning_list;
154 ifref_t *ifref;
155 ifref_list_t *ifref_list;
156 char *str;
157 UUID *uuid;
158 unsigned int num;
159 double dbl;
160 interface_info_t ifinfo;
161 typelib_t *typelib;
162 struct _import_t *import;
163 struct _decl_spec_t *declspec;
164 enum storage_class stgclass;
167 %token <str> aIDENTIFIER aPRAGMA
168 %token <str> aKNOWNTYPE
169 %token <num> aNUM aHEXNUM
170 %token <dbl> aDOUBLE
171 %token <str> aSTRING aWSTRING aSQSTRING
172 %token <uuid> aUUID
173 %token aEOF
174 %token SHL SHR
175 %token MEMBERPTR
176 %token EQUALITY INEQUALITY
177 %token GREATEREQUAL LESSEQUAL
178 %token LOGICALOR LOGICALAND
179 %token ELLIPSIS
180 %token tAGGREGATABLE tALLOCATE tANNOTATION tAPPOBJECT tASYNC tASYNCUUID
181 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
182 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
183 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
184 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
185 %token tDECODE tDEFAULT tDEFAULTBIND
186 %token tDEFAULTCOLLELEM
187 %token tDEFAULTVALUE
188 %token tDEFAULTVTABLE
189 %token tDISABLECONSISTENCYCHECK tDISPLAYBIND
190 %token tDISPINTERFACE
191 %token tDLLNAME tDOUBLE tDUAL
192 %token tENABLEALLOCATE tENCODE tENDPOINT
193 %token tENTRY tENUM tERRORSTATUST
194 %token tEXPLICITHANDLE tEXTERN
195 %token tFALSE
196 %token tFASTCALL tFAULTSTATUS
197 %token tFLOAT tFORCEALLOCATE
198 %token tHANDLE
199 %token tHANDLET
200 %token tHELPCONTEXT tHELPFILE
201 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
202 %token tHIDDEN
203 %token tHYPER tID tIDEMPOTENT
204 %token tIGNORE tIIDIS
205 %token tIMMEDIATEBIND
206 %token tIMPLICITHANDLE
207 %token tIMPORT tIMPORTLIB
208 %token tIN tIN_LINE tINLINE
209 %token tINPUTSYNC
210 %token tINT tINT3264 tINT64
211 %token tINTERFACE
212 %token tLCID
213 %token tLENGTHIS tLIBRARY
214 %token tLICENSED tLOCAL
215 %token tLONG
216 %token tMAYBE tMESSAGE
217 %token tMETHODS
218 %token tMODULE
219 %token tNAMESPACE
220 %token tNOCODE tNONBROWSABLE
221 %token tNONCREATABLE
222 %token tNONEXTENSIBLE
223 %token tNOTIFY tNOTIFYFLAG
224 %token tNULL
225 %token tOBJECT tODL tOLEAUTOMATION
226 %token tOPTIMIZE tOPTIONAL
227 %token tOUT
228 %token tPARTIALIGNORE tPASCAL
229 %token tPOINTERDEFAULT
230 %token tPRAGMA_WARNING
231 %token tPROGID tPROPERTIES
232 %token tPROPGET tPROPPUT tPROPPUTREF
233 %token tPROXY tPTR
234 %token tPUBLIC
235 %token tRANGE
236 %token tREADONLY tREF
237 %token tREGISTER tREPRESENTAS
238 %token tREQUESTEDIT
239 %token tRESTRICTED
240 %token tRETVAL
241 %token tSAFEARRAY
242 %token tSHORT
243 %token tSIGNED
244 %token tSIZEIS tSIZEOF
245 %token tSMALL
246 %token tSOURCE
247 %token tSTATIC
248 %token tSTDCALL
249 %token tSTRICTCONTEXTHANDLE
250 %token tSTRING tSTRUCT
251 %token tSWITCH tSWITCHIS tSWITCHTYPE
252 %token tTHREADING tTRANSMITAS
253 %token tTRUE
254 %token tTYPEDEF
255 %token tUIDEFAULT tUNION
256 %token tUNIQUE
257 %token tUNSIGNED
258 %token tUSESGETLASTERROR tUSERMARSHAL tUUID
259 %token tV1ENUM
260 %token tVARARG
261 %token tVERSION tVIPROGID
262 %token tVOID
263 %token tWCHAR tWIREMARSHAL
264 %token tAPARTMENT tNEUTRAL tSINGLE tFREE tBOTH
266 %type <attr> attribute type_qualifier function_specifier
267 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
268 %type <str_list> str_list
269 %type <expr> m_expr expr expr_const expr_int_const array m_bitfield
270 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
271 %type <ifinfo> interfacehdr
272 %type <stgclass> storage_cls_spec
273 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
274 %type <type> inherit interface interfacedef interfacedec
275 %type <type> dispinterface dispinterfacehdr dispinterfacedef
276 %type <type> module modulehdr moduledef
277 %type <str> namespacedef
278 %type <type> base_type int_std
279 %type <type> enumdef structdef uniondef typedecl
280 %type <type> type
281 %type <ifref> coclass_int
282 %type <ifref_list> coclass_ints
283 %type <var> arg ne_union_field union_field s_field case enum declaration
284 %type <var> funcdef
285 %type <var_list> m_args arg_list args dispint_meths
286 %type <var_list> fields ne_union_fields cases enums enum_list dispint_props field
287 %type <var> m_ident ident
288 %type <declarator> declarator direct_declarator init_declarator struct_declarator
289 %type <declarator> m_any_declarator any_declarator any_declarator_no_direct any_direct_declarator
290 %type <declarator> m_abstract_declarator abstract_declarator abstract_declarator_no_direct abstract_direct_declarator
291 %type <declarator_list> declarator_list struct_declarator_list
292 %type <type> coclass coclasshdr coclassdef
293 %type <num> pointer_type threading_type version
294 %type <str> libraryhdr callconv cppquote importlib import t_ident
295 %type <uuid> uuid_string
296 %type <import> import_start
297 %type <typelib> library_start librarydef
298 %type <statement> statement typedef pragma_warning
299 %type <stmt_list> gbl_statements imp_statements int_statements
300 %type <warning_list> warnings
302 %left ','
303 %right '?' ':'
304 %left LOGICALOR
305 %left LOGICALAND
306 %left '|'
307 %left '^'
308 %left '&'
309 %left EQUALITY INEQUALITY
310 %left '<' '>' LESSEQUAL GREATEREQUAL
311 %left SHL SHR
312 %left '-' '+'
313 %left '*' '/' '%'
314 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
315 %left '.' MEMBERPTR '[' ']'
317 %error-verbose
321 input: gbl_statements { fix_incomplete();
322 check_statements($1, FALSE);
323 check_all_user_types($1);
324 write_header($1);
325 write_id_data($1);
326 write_proxies($1);
327 write_client($1);
328 write_server($1);
329 write_regscript($1);
330 write_dlldata($1);
331 write_local_stubs($1);
335 gbl_statements: { $$ = NULL; }
336 | gbl_statements namespacedef '{' { push_namespace($2); } gbl_statements '}'
337 { pop_namespace($2); $$ = append_statements($1, $5); }
338 | gbl_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
339 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
340 | gbl_statements coclass ';' { $$ = $1;
341 reg_type($2, $2->name, current_namespace, 0);
343 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
344 reg_type($2, $2->name, current_namespace, 0);
346 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
347 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
348 | gbl_statements statement { $$ = append_statement($1, $2); }
351 imp_statements: { $$ = NULL; }
352 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
353 | imp_statements namespacedef '{' { push_namespace($2); } imp_statements '}'
354 { pop_namespace($2); $$ = append_statements($1, $5); }
355 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
356 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, current_namespace, 0); }
357 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
358 reg_type($2, $2->name, current_namespace, 0);
360 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
361 | imp_statements statement { $$ = append_statement($1, $2); }
362 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
363 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
366 int_statements: { $$ = NULL; }
367 | int_statements statement { $$ = append_statement($1, $2); }
370 semicolon_opt:
371 | ';'
374 statement:
375 cppquote { $$ = make_statement_cppquote($1); }
376 | typedecl ';' { $$ = make_statement_type_decl($1); }
377 | declaration ';' { $$ = make_statement_declaration($1); }
378 | import { $$ = make_statement_import($1); }
379 | typedef ';' { $$ = $1; }
380 | aPRAGMA { $$ = make_statement_pragma($1); }
381 | pragma_warning { $$ = NULL; }
384 pragma_warning: tPRAGMA_WARNING '(' aIDENTIFIER ':' warnings ')'
386 int result;
387 $$ = NULL;
388 result = do_warning($3, $5);
389 if(!result)
390 error_loc("expected \"disable\" or \"enable\"\n");
394 warnings:
395 aNUM { $$ = append_warning(NULL, $1); }
396 | warnings aNUM { $$ = append_warning($1, $2); }
399 typedecl:
400 enumdef
401 | tENUM aIDENTIFIER { $$ = type_new_enum($2, current_namespace, FALSE, NULL); }
402 | structdef
403 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, current_namespace, FALSE, NULL); }
404 | uniondef
405 | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, FALSE, NULL); }
406 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
407 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
408 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
411 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
413 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
414 $$ = xmalloc(sizeof(struct _import_t));
415 $$->name = $2;
416 $$->import_performed = do_import($2);
417 if (!$$->import_performed) yychar = aEOF;
421 import: import_start imp_statements aEOF { $$ = $1->name;
422 if ($1->import_performed) pop_import();
423 free($1);
427 importlib: tIMPORTLIB '(' aSTRING ')'
428 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
431 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
432 | tLIBRARY aKNOWNTYPE { $$ = $2; }
434 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
435 if (!parse_only) start_typelib($$);
438 librarydef: library_start imp_statements '}'
439 semicolon_opt { $$ = $1;
440 $$->stmts = $2;
441 if (!parse_only) end_typelib();
445 m_args: { $$ = NULL; }
446 | args
449 arg_list: arg { check_arg_attrs($1); $$ = append_var( NULL, $1 ); }
450 | arg_list ',' arg { check_arg_attrs($3); $$ = append_var( $1, $3 ); }
453 args: arg_list
454 | arg_list ',' ELLIPSIS { $$ = append_var( $1, make_var(strdup("...")) ); }
457 /* split into two rules to get bison to resolve a tVOID conflict */
458 arg: attributes decl_spec m_any_declarator { if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
459 error_loc("invalid storage class for function parameter\n");
460 $$ = declare_var($1, $2, $3, TRUE);
461 free($2); free($3);
463 | decl_spec m_any_declarator { if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
464 error_loc("invalid storage class for function parameter\n");
465 $$ = declare_var(NULL, $1, $2, TRUE);
466 free($1); free($2);
470 array: '[' expr ']' { $$ = $2;
471 if (!$$->is_const)
472 error_loc("array dimension is not an integer constant\n");
474 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
475 | '[' ']' { $$ = make_expr(EXPR_VOID); }
478 m_attributes: { $$ = NULL; }
479 | attributes
482 attributes:
483 '[' attrib_list ']' { $$ = $2; }
486 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
487 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
488 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
491 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
492 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
495 attribute: { $$ = NULL; }
496 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
497 | tANNOTATION '(' aSTRING ')' { $$ = make_attrp(ATTR_ANNOTATION, $3); }
498 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
499 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
500 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
501 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
502 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
503 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
504 | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
505 | tCODE { $$ = make_attr(ATTR_CODE); }
506 | tCOMMSTATUS { $$ = make_attr(ATTR_COMMSTATUS); }
507 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
508 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
509 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
510 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
511 | tDECODE { $$ = make_attr(ATTR_DECODE); }
512 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
513 | tDEFAULTBIND { $$ = make_attr(ATTR_DEFAULTBIND); }
514 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
515 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
516 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
517 | tDISABLECONSISTENCYCHECK { $$ = make_attr(ATTR_DISABLECONSISTENCYCHECK); }
518 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
519 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
520 | tDUAL { $$ = make_attr(ATTR_DUAL); }
521 | tENABLEALLOCATE { $$ = make_attr(ATTR_ENABLEALLOCATE); }
522 | tENCODE { $$ = make_attr(ATTR_ENCODE); }
523 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
524 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
525 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
526 | tFAULTSTATUS { $$ = make_attr(ATTR_FAULTSTATUS); }
527 | tFORCEALLOCATE { $$ = make_attr(ATTR_FORCEALLOCATE); }
528 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
529 | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
530 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
531 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
532 | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
533 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
534 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
535 | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
536 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
537 | tIGNORE { $$ = make_attr(ATTR_IGNORE); }
538 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
539 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
540 | tIMPLICITHANDLE '(' arg ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $3); }
541 | tIN { $$ = make_attr(ATTR_IN); }
542 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
543 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
544 | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
545 | tLCID { $$ = make_attr(ATTR_PARAMLCID); }
546 | tLICENSED { $$ = make_attr(ATTR_LICENSED); }
547 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
548 | tMAYBE { $$ = make_attr(ATTR_MAYBE); }
549 | tMESSAGE { $$ = make_attr(ATTR_MESSAGE); }
550 | tNOCODE { $$ = make_attr(ATTR_NOCODE); }
551 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
552 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
553 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
554 | tNOTIFY { $$ = make_attr(ATTR_NOTIFY); }
555 | tNOTIFYFLAG { $$ = make_attr(ATTR_NOTIFYFLAG); }
556 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
557 | tODL { $$ = make_attr(ATTR_ODL); }
558 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
559 | tOPTIMIZE '(' aSTRING ')' { $$ = make_attrp(ATTR_OPTIMIZE, $3); }
560 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
561 | tOUT { $$ = make_attr(ATTR_OUT); }
562 | tPARTIALIGNORE { $$ = make_attr(ATTR_PARTIALIGNORE); }
563 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
564 | tPROGID '(' aSTRING ')' { $$ = make_attrp(ATTR_PROGID, $3); }
565 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
566 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
567 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
568 | tPROXY { $$ = make_attr(ATTR_PROXY); }
569 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
570 | tRANGE '(' expr_int_const ',' expr_int_const ')'
571 { expr_list_t *list = append_expr( NULL, $3 );
572 list = append_expr( list, $5 );
573 $$ = make_attrp(ATTR_RANGE, list); }
574 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
575 | tREPRESENTAS '(' type ')' { $$ = make_attrp(ATTR_REPRESENTAS, $3); }
576 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
577 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
578 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
579 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
580 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
581 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
582 | tSTRING { $$ = make_attr(ATTR_STRING); }
583 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
584 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
585 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
586 | tTHREADING '(' threading_type ')' { $$ = make_attrv(ATTR_THREADING, $3); }
587 | tUIDEFAULT { $$ = make_attr(ATTR_UIDEFAULT); }
588 | tUSESGETLASTERROR { $$ = make_attr(ATTR_USESGETLASTERROR); }
589 | tUSERMARSHAL '(' type ')' { $$ = make_attrp(ATTR_USERMARSHAL, $3); }
590 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
591 | tASYNCUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_ASYNCUUID, $3); }
592 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
593 | tVARARG { $$ = make_attr(ATTR_VARARG); }
594 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
595 | tVIPROGID '(' aSTRING ')' { $$ = make_attrp(ATTR_VIPROGID, $3); }
596 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
597 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
600 uuid_string:
601 aUUID
602 | aSTRING { if (!is_valid_uuid($1))
603 error_loc("invalid UUID: %s\n", $1);
604 $$ = parse_uuid($1); }
607 callconv: tCDECL { $$ = xstrdup("__cdecl"); }
608 | tFASTCALL { $$ = xstrdup("__fastcall"); }
609 | tPASCAL { $$ = xstrdup("__pascal"); }
610 | tSTDCALL { $$ = xstrdup("__stdcall"); }
613 cases: { $$ = NULL; }
614 | cases case { $$ = append_var( $1, $2 ); }
617 case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
618 $$ = $4; if (!$$) $$ = make_var(NULL);
619 $$->attrs = append_attr( $$->attrs, a );
621 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
622 $$ = $3; if (!$$) $$ = make_var(NULL);
623 $$->attrs = append_attr( $$->attrs, a );
627 enums: { $$ = NULL; }
628 | enum_list ',' { $$ = $1; }
629 | enum_list
632 enum_list: enum { if (!$1->eval)
633 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
634 $$ = append_var( NULL, $1 );
636 | enum_list ',' enum { if (!$3->eval)
638 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
639 enum expr_type type = EXPR_NUM;
640 if (last->eval->type == EXPR_HEXNUM) type = EXPR_HEXNUM;
641 if (last->eval->cval + 1 < 0) type = EXPR_HEXNUM;
642 $3->eval = make_exprl(type, last->eval->cval + 1);
644 $$ = append_var( $1, $3 );
648 enum: ident '=' expr_int_const { $$ = reg_const($1);
649 $$->eval = $3;
650 $$->type = type_new_int(TYPE_BASIC_INT, 0);
652 | ident { $$ = reg_const($1);
653 $$->type = type_new_int(TYPE_BASIC_INT, 0);
657 enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, current_namespace, TRUE, $4); }
660 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
661 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
664 m_expr: { $$ = make_expr(EXPR_VOID); }
665 | expr
668 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
669 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
670 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
671 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
672 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
673 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
674 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
675 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
676 | aSQSTRING { $$ = make_exprs(EXPR_CHARCONST, $1); }
677 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
678 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
679 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
680 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
681 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
682 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
683 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
684 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
685 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
686 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
687 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
688 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
689 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
690 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
691 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
692 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
693 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
694 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
695 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
696 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
697 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
698 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
699 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
700 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
701 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
702 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
703 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
704 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
705 | '(' decl_spec m_abstract_declarator ')' expr %prec CAST
706 { $$ = make_exprt(EXPR_CAST, declare_var(NULL, $2, $3, 0), $5); free($2); free($3); }
707 | tSIZEOF '(' decl_spec m_abstract_declarator ')'
708 { $$ = make_exprt(EXPR_SIZEOF, declare_var(NULL, $3, $4, 0), NULL); free($3); free($4); }
709 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
710 | '(' expr ')' { $$ = $2; }
713 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
714 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
717 expr_int_const: expr { $$ = $1;
718 if (!$$->is_const)
719 error_loc("expression is not an integer constant\n");
723 expr_const: expr { $$ = $1;
724 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
725 error_loc("expression is not constant\n");
729 fields: { $$ = NULL; }
730 | fields field { $$ = append_var_list($1, $2); }
733 field: m_attributes decl_spec struct_declarator_list ';'
734 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
735 check_field_attrs(first, $1);
736 $$ = set_var_types($1, $2, $3);
738 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
739 v->type = $2; v->attrs = $1;
740 $$ = append_var(NULL, v);
744 ne_union_field:
745 s_field ';' { $$ = $1; }
746 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
749 ne_union_fields: { $$ = NULL; }
750 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
753 union_field:
754 s_field ';' { $$ = $1; }
755 | ';' { $$ = NULL; }
758 s_field: m_attributes decl_spec declarator { $$ = declare_var(check_field_attrs($3->var->name, $1),
759 $2, $3, FALSE);
760 free($3);
762 | m_attributes structdef { var_t *v = make_var(NULL);
763 v->type = $2; v->attrs = $1;
764 $$ = v;
768 funcdef: declaration { $$ = $1;
769 if (type_get_type($$->type) != TYPE_FUNCTION)
770 error_loc("only methods may be declared inside the methods section of a dispinterface\n");
771 check_function_attrs($$->name, $$->attrs);
775 declaration:
776 attributes decl_spec init_declarator
777 { $$ = declare_var($1, $2, $3, FALSE);
778 free($3);
780 | decl_spec init_declarator { $$ = declare_var(NULL, $1, $2, FALSE);
781 free($2);
785 m_ident: { $$ = NULL; }
786 | ident
789 t_ident: { $$ = NULL; }
790 | aIDENTIFIER { $$ = $1; }
791 | aKNOWNTYPE { $$ = $1; }
794 ident: aIDENTIFIER { $$ = make_var($1); }
795 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
796 | aKNOWNTYPE { $$ = make_var($<str>1); }
799 base_type: tBYTE { $$ = find_type_or_error($<str>1, 0); }
800 | tWCHAR { $$ = find_type_or_error($<str>1, 0); }
801 | int_std
802 | tSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), -1); }
803 | tUNSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), 1); }
804 | tUNSIGNED { $$ = type_new_int(TYPE_BASIC_INT, 1); }
805 | tFLOAT { $$ = find_type_or_error($<str>1, 0); }
806 | tDOUBLE { $$ = find_type_or_error($<str>1, 0); }
807 | tBOOLEAN { $$ = find_type_or_error($<str>1, 0); }
808 | tERRORSTATUST { $$ = find_type_or_error($<str>1, 0); }
809 | tHANDLET { $$ = find_type_or_error($<str>1, 0); }
812 m_int:
813 | tINT
816 int_std: tINT { $$ = type_new_int(TYPE_BASIC_INT, 0); }
817 | tSHORT m_int { $$ = type_new_int(TYPE_BASIC_INT16, 0); }
818 | tSMALL { $$ = type_new_int(TYPE_BASIC_INT8, 0); }
819 | tLONG m_int { $$ = type_new_int(TYPE_BASIC_INT32, 0); }
820 | tHYPER m_int { $$ = type_new_int(TYPE_BASIC_HYPER, 0); }
821 | tINT64 { $$ = type_new_int(TYPE_BASIC_INT64, 0); }
822 | tCHAR { $$ = type_new_int(TYPE_BASIC_CHAR, 0); }
823 | tINT3264 { $$ = type_new_int(TYPE_BASIC_INT3264, 0); }
826 coclass: tCOCLASS aIDENTIFIER { $$ = type_new_coclass($2); }
827 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, NULL, 0);
828 if (type_get_type_detect_alias($$) != TYPE_COCLASS)
829 error_loc("%s was not declared a coclass at %s:%d\n",
830 $2, $$->loc_info.input_name,
831 $$->loc_info.line_number);
835 coclasshdr: attributes coclass { $$ = $2;
836 check_def($$);
837 $$->attrs = check_coclass_attrs($2->name, $1);
841 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
842 { $$ = type_coclass_define($1, $3); }
845 namespacedef: tNAMESPACE aIDENTIFIER { $$ = $2; }
848 coclass_ints: { $$ = NULL; }
849 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
852 coclass_int:
853 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
856 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
857 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
860 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
861 $$ = $2;
862 check_def($$);
863 attrs = make_attr(ATTR_DISPINTERFACE);
864 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
865 $$->defined = TRUE;
869 dispint_props: tPROPERTIES ':' { $$ = NULL; }
870 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
873 dispint_meths: tMETHODS ':' { $$ = NULL; }
874 | dispint_meths funcdef ';' { $$ = append_var( $1, $2 ); }
877 dispinterfacedef: dispinterfacehdr '{'
878 dispint_props
879 dispint_meths
880 '}' { $$ = $1;
881 type_dispinterface_define($$, $3, $4);
883 | dispinterfacehdr
884 '{' interface ';' '}' { $$ = $1;
885 type_dispinterface_define_from_iface($$, $3);
889 inherit: { $$ = NULL; }
890 | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
893 interface: tINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
894 | tINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, current_namespace, 0); }
897 interfacehdr: attributes interface { $$.interface = $2;
898 $$.old_pointer_default = pointer_default;
899 if (is_attr($1, ATTR_POINTERDEFAULT))
900 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
901 check_def($2);
902 $2->attrs = check_iface_attrs($2->name, $1);
903 $2->defined = TRUE;
907 interfacedef: interfacehdr inherit
908 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
909 if($$ == $2)
910 error_loc("Interface can't inherit from itself\n");
911 type_interface_define($$, $2, $4);
912 pointer_default = $1.old_pointer_default;
914 /* MIDL is able to import the definition of a base class from inside the
915 * definition of a derived class, I'll try to support it with this rule */
916 | interfacehdr ':' aIDENTIFIER
917 '{' import int_statements '}'
918 semicolon_opt { $$ = $1.interface;
919 type_interface_define($$, find_type_or_error2($3, 0), $6);
920 pointer_default = $1.old_pointer_default;
922 | dispinterfacedef semicolon_opt { $$ = $1; }
925 interfacedec:
926 interface ';' { $$ = $1; }
927 | dispinterface ';' { $$ = $1; }
930 module: tMODULE aIDENTIFIER { $$ = type_new_module($2); }
931 | tMODULE aKNOWNTYPE { $$ = type_new_module($2); }
934 modulehdr: attributes module { $$ = $2;
935 $$->attrs = check_module_attrs($2->name, $1);
939 moduledef: modulehdr '{' int_statements '}'
940 semicolon_opt { $$ = $1;
941 type_module_define($$, $3);
945 storage_cls_spec:
946 tEXTERN { $$ = STG_EXTERN; }
947 | tSTATIC { $$ = STG_STATIC; }
948 | tREGISTER { $$ = STG_REGISTER; }
951 function_specifier:
952 tINLINE { $$ = make_attr(ATTR_INLINE); }
955 type_qualifier:
956 tCONST { $$ = make_attr(ATTR_CONST); }
959 m_type_qual_list: { $$ = NULL; }
960 | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
963 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
964 | decl_spec_no_type type m_decl_spec_no_type
965 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
968 m_decl_spec_no_type: { $$ = NULL; }
969 | decl_spec_no_type
972 decl_spec_no_type:
973 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
974 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
975 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
978 declarator:
979 '*' m_type_qual_list declarator %prec PPTR
980 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
981 | callconv declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
982 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
983 | direct_declarator
986 direct_declarator:
987 ident { $$ = make_declarator($1); }
988 | '(' declarator ')' { $$ = $2; }
989 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
990 | direct_declarator '(' m_args ')' { $$ = $1;
991 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
992 $$->type = NULL;
996 /* abstract declarator */
997 abstract_declarator:
998 '*' m_type_qual_list m_abstract_declarator %prec PPTR
999 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
1000 | callconv m_abstract_declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
1001 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1002 | abstract_direct_declarator
1005 /* abstract declarator without accepting direct declarator */
1006 abstract_declarator_no_direct:
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; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
1010 else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1013 /* abstract declarator or empty */
1014 m_abstract_declarator: { $$ = make_declarator(NULL); }
1015 | abstract_declarator
1018 /* abstract direct declarator */
1019 abstract_direct_declarator:
1020 '(' abstract_declarator_no_direct ')' { $$ = $2; }
1021 | abstract_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
1022 | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
1023 | '(' m_args ')'
1024 { $$ = make_declarator(NULL);
1025 $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
1026 $$->type = NULL;
1028 | abstract_direct_declarator '(' m_args ')'
1029 { $$ = $1;
1030 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
1031 $$->type = NULL;
1035 /* abstract or non-abstract declarator */
1036 any_declarator:
1037 '*' m_type_qual_list m_any_declarator %prec PPTR
1038 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
1039 | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1040 | any_direct_declarator
1043 /* abstract or non-abstract declarator without accepting direct declarator */
1044 any_declarator_no_direct:
1045 '*' m_type_qual_list m_any_declarator %prec PPTR
1046 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
1047 | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1050 /* abstract or non-abstract declarator or empty */
1051 m_any_declarator: { $$ = make_declarator(NULL); }
1052 | any_declarator
1055 /* abstract or non-abstract direct declarator. note: direct declarators
1056 * aren't accepted inside brackets to avoid ambiguity with the rule for
1057 * function arguments */
1058 any_direct_declarator:
1059 ident { $$ = make_declarator($1); }
1060 | '(' any_declarator_no_direct ')' { $$ = $2; }
1061 | any_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
1062 | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
1063 | '(' m_args ')'
1064 { $$ = make_declarator(NULL);
1065 $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
1066 $$->type = NULL;
1068 | any_direct_declarator '(' m_args ')'
1069 { $$ = $1;
1070 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
1071 $$->type = NULL;
1075 declarator_list:
1076 declarator { $$ = append_declarator( NULL, $1 ); }
1077 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
1080 m_bitfield: { $$ = NULL; }
1081 | ':' expr_const { $$ = $2; }
1084 struct_declarator: any_declarator m_bitfield { $$ = $1; $$->bits = $2;
1085 if (!$$->bits && !$$->var->name)
1086 error_loc("unnamed fields are not allowed\n");
1090 struct_declarator_list:
1091 struct_declarator { $$ = append_declarator( NULL, $1 ); }
1092 | struct_declarator_list ',' struct_declarator
1093 { $$ = append_declarator( $1, $3 ); }
1096 init_declarator:
1097 declarator { $$ = $1; }
1098 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
1101 threading_type:
1102 tAPARTMENT { $$ = THREADING_APARTMENT; }
1103 | tNEUTRAL { $$ = THREADING_NEUTRAL; }
1104 | tSINGLE { $$ = THREADING_SINGLE; }
1105 | tFREE { $$ = THREADING_FREE; }
1106 | tBOTH { $$ = THREADING_BOTH; }
1109 pointer_type:
1110 tREF { $$ = RPC_FC_RP; }
1111 | tUNIQUE { $$ = RPC_FC_UP; }
1112 | tPTR { $$ = RPC_FC_FP; }
1115 structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, current_namespace, TRUE, $4); }
1118 type: tVOID { $$ = type_new_void(); }
1119 | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
1120 | base_type { $$ = $1; }
1121 | enumdef { $$ = $1; }
1122 | tENUM aIDENTIFIER { $$ = type_new_enum($2, current_namespace, FALSE, NULL); }
1123 | structdef { $$ = $1; }
1124 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, current_namespace, FALSE, NULL); }
1125 | uniondef { $$ = $1; }
1126 | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, FALSE, NULL); }
1127 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
1130 typedef: m_attributes tTYPEDEF m_attributes decl_spec declarator_list
1131 { $1 = append_attribs($1, $3);
1132 reg_typedefs($4, $5, check_typedef_attrs($1));
1133 $$ = make_statement_typedef($5);
1137 uniondef: tUNION t_ident '{' ne_union_fields '}'
1138 { $$ = type_new_nonencapsulated_union($2, TRUE, $4); }
1139 | tUNION t_ident
1140 tSWITCH '(' s_field ')'
1141 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1144 version:
1145 aNUM { $$ = MAKEVERSION($1, 0); }
1146 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1147 | aHEXNUM { $$ = $1; }
1152 static void decl_builtin_basic(const char *name, enum type_basic_type type)
1154 type_t *t = type_new_basic(type);
1155 reg_type(t, name, NULL, 0);
1158 static void decl_builtin_alias(const char *name, type_t *t)
1160 reg_type(type_new_alias(t, name), name, NULL, 0);
1163 void init_types(void)
1165 decl_builtin_basic("byte", TYPE_BASIC_BYTE);
1166 decl_builtin_basic("wchar_t", TYPE_BASIC_WCHAR);
1167 decl_builtin_basic("float", TYPE_BASIC_FLOAT);
1168 decl_builtin_basic("double", TYPE_BASIC_DOUBLE);
1169 decl_builtin_basic("error_status_t", TYPE_BASIC_ERROR_STATUS_T);
1170 decl_builtin_basic("handle_t", TYPE_BASIC_HANDLE);
1171 decl_builtin_alias("boolean", type_new_basic(TYPE_BASIC_BYTE));
1174 static str_list_t *append_str(str_list_t *list, char *str)
1176 struct str_list_entry_t *entry;
1178 if (!str) return list;
1179 if (!list)
1181 list = xmalloc( sizeof(*list) );
1182 list_init( list );
1184 entry = xmalloc( sizeof(*entry) );
1185 entry->str = str;
1186 list_add_tail( list, &entry->entry );
1187 return list;
1190 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1192 attr_t *attr_existing;
1193 if (!attr) return list;
1194 if (!list)
1196 list = xmalloc( sizeof(*list) );
1197 list_init( list );
1199 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1200 if (attr_existing->type == attr->type)
1202 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1203 /* use the last attribute, like MIDL does */
1204 list_remove(&attr_existing->entry);
1205 break;
1207 list_add_tail( list, &attr->entry );
1208 return list;
1211 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1213 attr_t *attr;
1214 if (!src) return dst;
1215 LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1216 if (attr->type == type)
1218 list_remove(&attr->entry);
1219 return append_attr(dst, attr);
1221 return dst;
1224 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1226 struct list *entry;
1228 if (!old_list) return new_list;
1230 while ((entry = list_head(old_list)))
1232 attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1233 list_remove(entry);
1234 new_list = append_attr(new_list, attr);
1236 return new_list;
1239 static attr_list_t *dupattrs(const attr_list_t *list)
1241 attr_list_t *new_list;
1242 const attr_t *attr;
1244 if (!list) return NULL;
1246 new_list = xmalloc( sizeof(*list) );
1247 list_init( new_list );
1248 LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1250 attr_t *new_attr = xmalloc(sizeof(*new_attr));
1251 *new_attr = *attr;
1252 list_add_tail(new_list, &new_attr->entry);
1254 return new_list;
1257 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)
1259 decl_spec_t *declspec = left ? left : right;
1260 if (!declspec)
1262 declspec = xmalloc(sizeof(*declspec));
1263 declspec->type = NULL;
1264 declspec->attrs = NULL;
1265 declspec->stgclass = STG_NONE;
1267 declspec->type = type;
1268 if (left && declspec != left)
1270 declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1271 if (declspec->stgclass == STG_NONE)
1272 declspec->stgclass = left->stgclass;
1273 else if (left->stgclass != STG_NONE)
1274 error_loc("only one storage class can be specified\n");
1275 assert(!left->type);
1276 free(left);
1278 if (right && declspec != right)
1280 declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1281 if (declspec->stgclass == STG_NONE)
1282 declspec->stgclass = right->stgclass;
1283 else if (right->stgclass != STG_NONE)
1284 error_loc("only one storage class can be specified\n");
1285 assert(!right->type);
1286 free(right);
1289 declspec->attrs = append_attr(declspec->attrs, attr);
1290 if (declspec->stgclass == STG_NONE)
1291 declspec->stgclass = stgclass;
1292 else if (stgclass != STG_NONE)
1293 error_loc("only one storage class can be specified\n");
1295 /* apply attributes to type */
1296 if (type && declspec->attrs)
1298 attr_list_t *attrs;
1299 declspec->type = duptype(type, 1);
1300 attrs = dupattrs(type->attrs);
1301 declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1302 declspec->attrs = NULL;
1305 return declspec;
1308 static attr_t *make_attr(enum attr_type type)
1310 attr_t *a = xmalloc(sizeof(attr_t));
1311 a->type = type;
1312 a->u.ival = 0;
1313 return a;
1316 static attr_t *make_attrv(enum attr_type type, unsigned int val)
1318 attr_t *a = xmalloc(sizeof(attr_t));
1319 a->type = type;
1320 a->u.ival = val;
1321 return a;
1324 static attr_t *make_attrp(enum attr_type type, void *val)
1326 attr_t *a = xmalloc(sizeof(attr_t));
1327 a->type = type;
1328 a->u.pval = val;
1329 return a;
1332 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1334 if (!expr) return list;
1335 if (!list)
1337 list = xmalloc( sizeof(*list) );
1338 list_init( list );
1340 list_add_tail( list, &expr->entry );
1341 return list;
1344 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1346 if (!expr) return list;
1347 if (!list)
1349 list = xmalloc( sizeof(*list) );
1350 list_init( list );
1352 list_add_tail( list, &expr->entry );
1353 return list;
1356 static struct list type_pool = LIST_INIT(type_pool);
1357 typedef struct
1359 type_t data;
1360 struct list link;
1361 } type_pool_node_t;
1363 type_t *alloc_type(void)
1365 type_pool_node_t *node = xmalloc(sizeof *node);
1366 list_add_tail(&type_pool, &node->link);
1367 return &node->data;
1370 void set_all_tfswrite(int val)
1372 type_pool_node_t *node;
1373 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1374 node->data.tfswrite = val;
1377 void clear_all_offsets(void)
1379 type_pool_node_t *node;
1380 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1381 node->data.typestring_offset = node->data.ptrdesc = 0;
1384 static void type_function_add_head_arg(type_t *type, var_t *arg)
1386 if (!type->details.function->args)
1388 type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
1389 list_init( type->details.function->args );
1391 list_add_head( type->details.function->args, &arg->entry );
1394 static int is_allowed_range_type(const type_t *type)
1396 switch (type_get_type(type))
1398 case TYPE_ENUM:
1399 return TRUE;
1400 case TYPE_BASIC:
1401 switch (type_basic_get_type(type))
1403 case TYPE_BASIC_INT8:
1404 case TYPE_BASIC_INT16:
1405 case TYPE_BASIC_INT32:
1406 case TYPE_BASIC_INT64:
1407 case TYPE_BASIC_INT:
1408 case TYPE_BASIC_INT3264:
1409 case TYPE_BASIC_BYTE:
1410 case TYPE_BASIC_CHAR:
1411 case TYPE_BASIC_WCHAR:
1412 case TYPE_BASIC_HYPER:
1413 return TRUE;
1414 case TYPE_BASIC_FLOAT:
1415 case TYPE_BASIC_DOUBLE:
1416 case TYPE_BASIC_ERROR_STATUS_T:
1417 case TYPE_BASIC_HANDLE:
1418 return FALSE;
1420 return FALSE;
1421 default:
1422 return FALSE;
1426 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1428 type_t *ptrchain_type;
1429 if (!ptrchain)
1430 return type;
1431 for (ptrchain_type = ptrchain; type_pointer_get_ref(ptrchain_type); ptrchain_type = type_pointer_get_ref(ptrchain_type))
1433 assert(ptrchain_type->type_type == TYPE_POINTER);
1434 ptrchain_type->details.pointer.ref = type;
1435 return ptrchain;
1438 static warning_list_t *append_warning(warning_list_t *list, int num)
1440 warning_t *entry;
1442 if(!list)
1444 list = xmalloc( sizeof(*list) );
1445 list_init( list );
1447 entry = xmalloc( sizeof(*entry) );
1448 entry->num = num;
1449 list_add_tail( list, &entry->entry );
1450 return list;
1453 static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl,
1454 int top)
1456 var_t *v = decl->var;
1457 expr_list_t *sizes = get_attrp(attrs, ATTR_SIZEIS);
1458 expr_list_t *lengs = get_attrp(attrs, ATTR_LENGTHIS);
1459 int sizeless;
1460 expr_t *dim;
1461 type_t **ptype;
1462 array_dims_t *arr = decl ? decl->array : NULL;
1463 type_t *func_type = decl ? decl->func_type : NULL;
1464 type_t *type = decl_spec->type;
1466 if (is_attr(type->attrs, ATTR_INLINE))
1468 if (!func_type)
1469 error_loc("inline attribute applied to non-function type\n");
1470 else
1472 type_t *t;
1473 /* move inline attribute from return type node to function node */
1474 for (t = func_type; is_ptr(t); t = type_pointer_get_ref(t))
1476 t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1480 /* add type onto the end of the pointers in pident->type */
1481 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1482 v->stgclass = decl_spec->stgclass;
1483 v->attrs = attrs;
1485 /* check for pointer attribute being applied to non-pointer, non-array
1486 * type */
1487 if (!arr)
1489 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1490 const type_t *ptr = NULL;
1491 /* pointer attributes on the left side of the type belong to the function
1492 * pointer, if one is being declared */
1493 type_t **pt = func_type ? &func_type : &v->type;
1494 for (ptr = *pt; ptr && !ptr_attr; )
1496 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1497 if (!ptr_attr && type_is_alias(ptr))
1498 ptr = type_alias_get_aliasee(ptr);
1499 else
1500 break;
1502 if (is_ptr(ptr))
1504 if (ptr_attr && ptr_attr != RPC_FC_UP &&
1505 type_get_type(type_pointer_get_ref(ptr)) == TYPE_INTERFACE)
1506 warning_loc_info(&v->loc_info,
1507 "%s: pointer attribute applied to interface "
1508 "pointer type has no effect\n", v->name);
1509 if (!ptr_attr && top && (*pt)->details.pointer.def_fc != RPC_FC_RP)
1511 /* FIXME: this is a horrible hack to cope with the issue that we
1512 * store an offset to the typeformat string in the type object, but
1513 * two typeformat strings may be written depending on whether the
1514 * pointer is a toplevel parameter or not */
1515 *pt = duptype(*pt, 1);
1518 else if (ptr_attr)
1519 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1522 if (is_attr(v->attrs, ATTR_STRING))
1524 type_t *t = type;
1526 if (!is_ptr(v->type) && !arr)
1527 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1528 v->name);
1530 while (is_ptr(t))
1531 t = type_pointer_get_ref(t);
1533 if (type_get_type(t) != TYPE_BASIC &&
1534 (get_basic_fc(t) != RPC_FC_CHAR &&
1535 get_basic_fc(t) != RPC_FC_BYTE &&
1536 get_basic_fc(t) != RPC_FC_WCHAR))
1538 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1539 v->name);
1543 if (is_attr(v->attrs, ATTR_V1ENUM))
1545 if (type_get_type_detect_alias(v->type) != TYPE_ENUM)
1546 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1549 if (is_attr(v->attrs, ATTR_RANGE) && !is_allowed_range_type(v->type))
1550 error_loc("'%s': [range] attribute applied to non-integer type\n",
1551 v->name);
1553 ptype = &v->type;
1554 sizeless = FALSE;
1555 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1557 if (sizeless)
1558 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1560 if (dim->is_const)
1562 if (dim->cval <= 0)
1563 error_loc("%s: array dimension must be positive\n", v->name);
1565 /* FIXME: should use a type_memsize that allows us to pass in a pointer size */
1566 if (0)
1568 unsigned int size = type_memsize(v->type);
1570 if (0xffffffffu / size < dim->cval)
1571 error_loc("%s: total array size is too large\n", v->name);
1574 else
1575 sizeless = TRUE;
1577 *ptype = type_new_array(NULL, *ptype, FALSE,
1578 dim->is_const ? dim->cval : 0,
1579 dim->is_const ? NULL : dim, NULL,
1580 pointer_default);
1583 ptype = &v->type;
1584 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1586 if (dim->type != EXPR_VOID)
1588 if (is_array(*ptype))
1590 if (!type_array_get_conformance(*ptype) ||
1591 type_array_get_conformance(*ptype)->type != EXPR_VOID)
1592 error_loc("%s: cannot specify size_is for an already sized array\n", v->name);
1593 else
1594 *ptype = type_new_array((*ptype)->name,
1595 type_array_get_element(*ptype), FALSE,
1596 0, dim, NULL, 0);
1598 else if (is_ptr(*ptype))
1599 *ptype = type_new_array((*ptype)->name, type_pointer_get_ref(*ptype), TRUE,
1600 0, dim, NULL, pointer_default);
1601 else
1602 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1605 if (is_ptr(*ptype))
1606 ptype = &(*ptype)->details.pointer.ref;
1607 else if (is_array(*ptype))
1608 ptype = &(*ptype)->details.array.elem;
1609 else
1610 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1613 ptype = &v->type;
1614 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1616 if (dim->type != EXPR_VOID)
1618 if (is_array(*ptype))
1620 *ptype = type_new_array((*ptype)->name,
1621 type_array_get_element(*ptype),
1622 type_array_is_decl_as_ptr(*ptype),
1623 type_array_get_dim(*ptype),
1624 type_array_get_conformance(*ptype),
1625 dim, type_array_get_ptr_default_fc(*ptype));
1627 else
1628 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1631 if (is_ptr(*ptype))
1632 ptype = &(*ptype)->details.pointer.ref;
1633 else if (is_array(*ptype))
1634 ptype = &(*ptype)->details.array.elem;
1635 else
1636 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1639 /* v->type is currently pointing to the type on the left-side of the
1640 * declaration, so we need to fix this up so that it is the return type of the
1641 * function and make v->type point to the function side of the declaration */
1642 if (func_type)
1644 type_t *ft, *t;
1645 type_t *return_type = v->type;
1646 v->type = func_type;
1647 for (ft = v->type; is_ptr(ft); ft = type_pointer_get_ref(ft))
1649 assert(type_get_type_detect_alias(ft) == TYPE_FUNCTION);
1650 ft->details.function->retval = make_var(xstrdup("_RetVal"));
1651 ft->details.function->retval->type = return_type;
1652 /* move calling convention attribute, if present, from pointer nodes to
1653 * function node */
1654 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1655 ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1657 else
1659 type_t *t;
1660 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1661 if (is_attr(t->attrs, ATTR_CALLCONV))
1662 error_loc("calling convention applied to non-function-pointer type\n");
1665 if (decl->bits)
1666 v->type = type_new_bitfield(v->type, decl->bits);
1668 return v;
1671 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1673 declarator_t *decl, *next;
1674 var_list_t *var_list = NULL;
1676 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1678 var_t *var = declare_var(attrs, decl_spec, decl, 0);
1679 var_list = append_var(var_list, var);
1680 free(decl);
1682 free(decl_spec);
1683 return var_list;
1686 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1688 if (!iface) return list;
1689 if (!list)
1691 list = xmalloc( sizeof(*list) );
1692 list_init( list );
1694 list_add_tail( list, &iface->entry );
1695 return list;
1698 static ifref_t *make_ifref(type_t *iface)
1700 ifref_t *l = xmalloc(sizeof(ifref_t));
1701 l->iface = iface;
1702 l->attrs = NULL;
1703 return l;
1706 var_list_t *append_var(var_list_t *list, var_t *var)
1708 if (!var) return list;
1709 if (!list)
1711 list = xmalloc( sizeof(*list) );
1712 list_init( list );
1714 list_add_tail( list, &var->entry );
1715 return list;
1718 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1720 if (!vars) return list;
1721 if (!list)
1723 list = xmalloc( sizeof(*list) );
1724 list_init( list );
1726 list_move_tail( list, vars );
1727 return list;
1730 var_t *make_var(char *name)
1732 var_t *v = xmalloc(sizeof(var_t));
1733 v->name = name;
1734 v->type = NULL;
1735 v->attrs = NULL;
1736 v->eval = NULL;
1737 v->stgclass = STG_NONE;
1738 init_loc_info(&v->loc_info);
1739 return v;
1742 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1744 if (!d) return list;
1745 if (!list) {
1746 list = xmalloc(sizeof(*list));
1747 list_init(list);
1749 list_add_tail(list, &d->entry);
1750 return list;
1753 static declarator_t *make_declarator(var_t *var)
1755 declarator_t *d = xmalloc(sizeof(*d));
1756 d->var = var ? var : make_var(NULL);
1757 d->type = NULL;
1758 d->func_type = NULL;
1759 d->array = NULL;
1760 d->bits = NULL;
1761 return d;
1764 static type_t *make_safearray(type_t *type)
1766 return type_new_array(NULL, type_new_alias(type, "SAFEARRAY"), TRUE, 0,
1767 NULL, NULL, RPC_FC_RP);
1770 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1772 typelib_t *typelib = xmalloc(sizeof(*typelib));
1773 typelib->name = xstrdup(name);
1774 typelib->attrs = attrs;
1775 list_init( &typelib->importlibs );
1776 return typelib;
1779 static int hash_ident(const char *name)
1781 const char *p = name;
1782 int sum = 0;
1783 /* a simple sum hash is probably good enough */
1784 while (*p) {
1785 sum += *p;
1786 p++;
1788 return sum & (HASHMAX-1);
1791 /***** type repository *****/
1793 static struct namespace *find_sub_namespace(struct namespace *namespace, const char *name)
1795 struct namespace *cur;
1797 LIST_FOR_EACH_ENTRY(cur, &namespace->children, struct namespace, entry) {
1798 if(!strcmp(cur->name, name))
1799 return cur;
1802 return NULL;
1805 static void push_namespace(const char *name)
1807 struct namespace *namespace;
1809 namespace = find_sub_namespace(current_namespace, name);
1810 if(!namespace) {
1811 namespace = xmalloc(sizeof(*namespace));
1812 namespace->name = xstrdup(name);
1813 namespace->parent = current_namespace;
1814 list_add_tail(&current_namespace->children, &namespace->entry);
1815 list_init(&namespace->children);
1816 memset(namespace->type_hash, 0, sizeof(namespace->type_hash));
1819 current_namespace = namespace;
1822 static void pop_namespace(const char *name)
1824 assert(!strcmp(current_namespace->name, name) && current_namespace->parent);
1825 current_namespace = current_namespace->parent;
1828 struct rtype {
1829 const char *name;
1830 type_t *type;
1831 int t;
1832 struct rtype *next;
1835 type_t *reg_type(type_t *type, const char *name, struct namespace *namespace, int t)
1837 struct rtype *nt;
1838 int hash;
1839 if (!name) {
1840 error_loc("registering named type without name\n");
1841 return type;
1843 if (!namespace)
1844 namespace = &global_namespace;
1845 hash = hash_ident(name);
1846 nt = xmalloc(sizeof(struct rtype));
1847 nt->name = name;
1848 if (is_global_namespace(namespace))
1849 type->c_name = name;
1850 else
1851 type->c_name = format_namespace(namespace, "__x_", "_C", name);
1852 nt->type = type;
1853 nt->t = t;
1854 nt->next = namespace->type_hash[hash];
1855 namespace->type_hash[hash] = nt;
1856 if ((t == tsSTRUCT || t == tsUNION))
1857 fix_incomplete_types(type);
1858 return type;
1861 static int is_incomplete(const type_t *t)
1863 return !t->defined &&
1864 (type_get_type_detect_alias(t) == TYPE_STRUCT ||
1865 type_get_type_detect_alias(t) == TYPE_UNION ||
1866 type_get_type_detect_alias(t) == TYPE_ENCAPSULATED_UNION);
1869 void add_incomplete(type_t *t)
1871 struct typenode *tn = xmalloc(sizeof *tn);
1872 tn->type = t;
1873 list_add_tail(&incomplete_types, &tn->entry);
1876 static void fix_type(type_t *t)
1878 if (type_is_alias(t) && is_incomplete(t)) {
1879 type_t *ot = type_alias_get_aliasee(t);
1880 fix_type(ot);
1881 if (type_get_type_detect_alias(ot) == TYPE_STRUCT ||
1882 type_get_type_detect_alias(ot) == TYPE_UNION ||
1883 type_get_type_detect_alias(ot) == TYPE_ENCAPSULATED_UNION)
1884 t->details.structure = ot->details.structure;
1885 t->defined = ot->defined;
1889 static void fix_incomplete(void)
1891 struct typenode *tn, *next;
1893 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1894 fix_type(tn->type);
1895 list_remove(&tn->entry);
1896 free(tn);
1900 static void fix_incomplete_types(type_t *complete_type)
1902 struct typenode *tn, *next;
1904 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry)
1906 if (type_is_equal(complete_type, tn->type))
1908 tn->type->details.structure = complete_type->details.structure;
1909 list_remove(&tn->entry);
1910 free(tn);
1915 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1917 const declarator_t *decl;
1918 type_t *type = decl_spec->type;
1920 if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1921 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1923 /* We must generate names for tagless enum, struct or union.
1924 Typedef-ing a tagless enum, struct or union means we want the typedef
1925 to be included in a library hence the public attribute. */
1926 if (type_get_type_detect_alias(type) == TYPE_ENUM ||
1927 type_get_type_detect_alias(type) == TYPE_STRUCT ||
1928 type_get_type_detect_alias(type) == TYPE_UNION ||
1929 type_get_type_detect_alias(type) == TYPE_ENCAPSULATED_UNION)
1931 if (!type->name)
1932 type->name = gen_name();
1934 /* replace existing attributes when generating a typelib */
1935 if (do_typelib)
1936 type->attrs = attrs;
1939 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1942 if (decl->var->name) {
1943 type_t *cur;
1944 var_t *name;
1946 cur = find_type(decl->var->name, current_namespace, 0);
1949 * MIDL allows shadowing types that are declared in imported files.
1950 * We don't throw an error in this case and instead add a new type
1951 * (which is earlier on the list in hash table, so it will be used
1952 * instead of shadowed type).
1954 * FIXME: We may consider string separated type tables for each input
1955 * for cleaner solution.
1957 if (cur && input_name == cur->loc_info.input_name)
1958 error_loc("%s: redefinition error; original definition was at %s:%d\n",
1959 cur->name, cur->loc_info.input_name,
1960 cur->loc_info.line_number);
1962 name = declare_var(attrs, decl_spec, decl, 0);
1963 cur = type_new_alias(name->type, name->name);
1964 cur->attrs = attrs;
1966 if (is_incomplete(cur))
1967 add_incomplete(cur);
1968 reg_type(cur, cur->name, current_namespace, 0);
1971 return type;
1974 type_t *find_type(const char *name, struct namespace *namespace, int t)
1976 struct rtype *cur;
1978 if(namespace && namespace != &global_namespace) {
1979 for(cur = namespace->type_hash[hash_ident(name)]; cur; cur = cur->next) {
1980 if(cur->t == t && !strcmp(cur->name, name))
1981 return cur->type;
1984 for(cur = global_namespace.type_hash[hash_ident(name)]; cur; cur = cur->next) {
1985 if(cur->t == t && !strcmp(cur->name, name))
1986 return cur->type;
1988 return NULL;
1991 static type_t *find_type_or_error(const char *name, int t)
1993 type_t *type = find_type(name, NULL, t);
1994 if (!type) {
1995 error_loc("type '%s' not found\n", name);
1996 return NULL;
1998 return type;
2001 static type_t *find_type_or_error2(char *name, int t)
2003 type_t *tp = find_type_or_error(name, t);
2004 free(name);
2005 return tp;
2008 int is_type(const char *name)
2010 return find_type(name, current_namespace, 0) != NULL;
2013 type_t *get_type(enum type_type type, char *name, struct namespace *namespace, int t)
2015 type_t *tp;
2016 if (!namespace)
2017 namespace = &global_namespace;
2018 if (name) {
2019 tp = find_type(name, namespace, t);
2020 if (tp) {
2021 free(name);
2022 return tp;
2025 tp = make_type(type);
2026 tp->name = name;
2027 tp->namespace = namespace;
2028 if (!name) return tp;
2029 return reg_type(tp, name, namespace, t);
2032 /***** constant repository *****/
2034 struct rconst {
2035 char *name;
2036 var_t *var;
2037 struct rconst *next;
2040 struct rconst *const_hash[HASHMAX];
2042 static var_t *reg_const(var_t *var)
2044 struct rconst *nc;
2045 int hash;
2046 if (!var->name) {
2047 error_loc("registering constant without name\n");
2048 return var;
2050 hash = hash_ident(var->name);
2051 nc = xmalloc(sizeof(struct rconst));
2052 nc->name = var->name;
2053 nc->var = var;
2054 nc->next = const_hash[hash];
2055 const_hash[hash] = nc;
2056 return var;
2059 var_t *find_const(const char *name, int f)
2061 struct rconst *cur = const_hash[hash_ident(name)];
2062 while (cur && strcmp(cur->name, name))
2063 cur = cur->next;
2064 if (!cur) {
2065 if (f) error_loc("constant '%s' not found\n", name);
2066 return NULL;
2068 return cur->var;
2071 static char *gen_name(void)
2073 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2074 static unsigned long n = 0;
2075 static const char *file_id;
2076 static size_t size;
2077 char *name;
2079 if (! file_id)
2081 char *dst = dup_basename(input_idl_name, ".idl");
2082 file_id = dst;
2084 for (; *dst; ++dst)
2085 if (! isalnum((unsigned char) *dst))
2086 *dst = '_';
2088 size = sizeof format - 7 + strlen(file_id) + 8;
2091 name = xmalloc(size);
2092 sprintf(name, format, file_id, n++);
2093 return name;
2096 struct allowed_attr
2098 unsigned int dce_compatible : 1;
2099 unsigned int acf : 1;
2100 unsigned int on_interface : 1;
2101 unsigned int on_function : 1;
2102 unsigned int on_arg : 1;
2103 unsigned int on_type : 1;
2104 unsigned int on_enum : 1;
2105 unsigned int on_struct : 2;
2106 unsigned int on_union : 1;
2107 unsigned int on_field : 1;
2108 unsigned int on_library : 1;
2109 unsigned int on_dispinterface : 1;
2110 unsigned int on_module : 1;
2111 unsigned int on_coclass : 1;
2112 const char *display_name;
2115 struct allowed_attr allowed_attr[] =
2117 /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
2118 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2119 /* ATTR_ANNOTATION */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "annotation" },
2120 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2121 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
2122 /* ATTR_ASYNCUUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "async_uuid" },
2123 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2124 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2125 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2126 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2127 /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2128 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2129 /* ATTR_CODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "code" },
2130 /* ATTR_COMMSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "comm_status" },
2131 /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
2132 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
2133 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2134 /* ATTR_DECODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "decode" },
2135 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
2136 /* ATTR_DEFAULTBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultbind" },
2137 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2138 /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2139 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2140 /* ATTR_DISABLECONSISTENCYCHECK */{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "disable_consistency_check" },
2141 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2142 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2143 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2144 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2145 /* ATTR_ENABLEALLOCATE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "enable_allocate" },
2146 /* ATTR_ENCODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "encode" },
2147 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2148 /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2149 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2150 /* ATTR_FAULTSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "fault_status" },
2151 /* ATTR_FORCEALLOCATE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "force_allocate" },
2152 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2153 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2154 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2155 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2156 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2157 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2158 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2159 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, "id" },
2160 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2161 /* ATTR_IGNORE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "ignore" },
2162 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2163 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2164 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2165 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2166 /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2167 /* ATTR_INPUTSYNC */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2168 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2169 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2170 /* ATTR_LICENSED */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "licensed" },
2171 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2172 /* ATTR_MAYBE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "maybe" },
2173 /* ATTR_MESSAGE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "message" },
2174 /* ATTR_NOCODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nocode" },
2175 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2176 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2177 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2178 /* ATTR_NOTIFY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify" },
2179 /* ATTR_NOTIFYFLAG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify_flag" },
2180 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2181 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2182 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2183 /* ATTR_OPTIMIZE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optimize" },
2184 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2185 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2186 /* ATTR_PARAMLCID */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "lcid" },
2187 /* ATTR_PARTIALIGNORE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "partial_ignore" },
2188 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2189 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2190 /* ATTR_PROGID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "progid" },
2191 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2192 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2193 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2194 /* ATTR_PROXY */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "proxy" },
2195 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2196 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2197 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2198 /* ATTR_REPRESENTAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "represent_as" },
2199 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2200 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2201 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2202 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2203 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2204 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2205 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2206 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2207 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2208 /* ATTR_THREADING */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "threading" },
2209 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2210 /* ATTR_UIDEFAULT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "uidefault" },
2211 /* ATTR_USESGETLASTERROR */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "usesgetlasterror" },
2212 /* ATTR_USERMARSHAL */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "user_marshal" },
2213 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, "uuid" },
2214 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2215 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2216 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 1, 1, 2, 0, 0, 1, 0, 0, 1, "version" },
2217 /* ATTR_VIPROGID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "vi_progid" },
2218 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2221 const char *get_attr_display_name(enum attr_type type)
2223 return allowed_attr[type].display_name;
2226 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2228 const attr_t *attr;
2229 if (!attrs) return attrs;
2230 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2232 if (!allowed_attr[attr->type].on_interface)
2233 error_loc("inapplicable attribute %s for interface %s\n",
2234 allowed_attr[attr->type].display_name, name);
2235 if (attr->type == ATTR_IMPLICIT_HANDLE)
2237 const var_t *var = attr->u.pval;
2238 if (type_get_type( var->type) == TYPE_BASIC &&
2239 type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
2240 continue;
2241 if (is_aliaschain_attr( var->type, ATTR_HANDLE ))
2242 continue;
2243 error_loc("attribute %s requires a handle type in interface %s\n",
2244 allowed_attr[attr->type].display_name, name);
2247 return attrs;
2250 static attr_list_t *check_function_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_function)
2257 error_loc("inapplicable attribute %s for function %s\n",
2258 allowed_attr[attr->type].display_name, name);
2260 return attrs;
2263 static void check_arg_attrs(const var_t *arg)
2265 const attr_t *attr;
2267 if (arg->attrs)
2269 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2271 if (!allowed_attr[attr->type].on_arg)
2272 error_loc("inapplicable attribute %s for argument %s\n",
2273 allowed_attr[attr->type].display_name, arg->name);
2278 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2280 const attr_t *attr;
2281 if (!attrs) return attrs;
2282 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2284 if (!allowed_attr[attr->type].on_type)
2285 error_loc("inapplicable attribute %s for typedef\n",
2286 allowed_attr[attr->type].display_name);
2288 return attrs;
2291 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2293 const attr_t *attr;
2294 if (!attrs) return attrs;
2295 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2297 if (!allowed_attr[attr->type].on_enum)
2298 error_loc("inapplicable attribute %s for enum\n",
2299 allowed_attr[attr->type].display_name);
2301 return attrs;
2304 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2306 int mask = winrt_mode ? 3 : 1;
2307 const attr_t *attr;
2308 if (!attrs) return attrs;
2309 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2311 if (!(allowed_attr[attr->type].on_struct & mask))
2312 error_loc("inapplicable attribute %s for struct\n",
2313 allowed_attr[attr->type].display_name);
2315 return attrs;
2318 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2320 const attr_t *attr;
2321 if (!attrs) return attrs;
2322 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2324 if (!allowed_attr[attr->type].on_union)
2325 error_loc("inapplicable attribute %s for union\n",
2326 allowed_attr[attr->type].display_name);
2328 return attrs;
2331 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2333 const attr_t *attr;
2334 if (!attrs) return attrs;
2335 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2337 if (!allowed_attr[attr->type].on_field)
2338 error_loc("inapplicable attribute %s for field %s\n",
2339 allowed_attr[attr->type].display_name, name);
2341 return attrs;
2344 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2346 const attr_t *attr;
2347 if (!attrs) return attrs;
2348 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2350 if (!allowed_attr[attr->type].on_library)
2351 error_loc("inapplicable attribute %s for library %s\n",
2352 allowed_attr[attr->type].display_name, name);
2354 return attrs;
2357 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2359 const attr_t *attr;
2360 if (!attrs) return attrs;
2361 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2363 if (!allowed_attr[attr->type].on_dispinterface)
2364 error_loc("inapplicable attribute %s for dispinterface %s\n",
2365 allowed_attr[attr->type].display_name, name);
2367 return attrs;
2370 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2372 const attr_t *attr;
2373 if (!attrs) return attrs;
2374 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2376 if (!allowed_attr[attr->type].on_module)
2377 error_loc("inapplicable attribute %s for module %s\n",
2378 allowed_attr[attr->type].display_name, name);
2380 return attrs;
2383 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2385 const attr_t *attr;
2386 if (!attrs) return attrs;
2387 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2389 if (!allowed_attr[attr->type].on_coclass)
2390 error_loc("inapplicable attribute %s for coclass %s\n",
2391 allowed_attr[attr->type].display_name, name);
2393 return attrs;
2396 static int is_allowed_conf_type(const type_t *type)
2398 switch (type_get_type(type))
2400 case TYPE_ENUM:
2401 return TRUE;
2402 case TYPE_BASIC:
2403 switch (type_basic_get_type(type))
2405 case TYPE_BASIC_INT8:
2406 case TYPE_BASIC_INT16:
2407 case TYPE_BASIC_INT32:
2408 case TYPE_BASIC_INT64:
2409 case TYPE_BASIC_INT:
2410 case TYPE_BASIC_CHAR:
2411 case TYPE_BASIC_HYPER:
2412 case TYPE_BASIC_BYTE:
2413 case TYPE_BASIC_WCHAR:
2414 return TRUE;
2415 default:
2416 return FALSE;
2418 case TYPE_ALIAS:
2419 /* shouldn't get here because of type_get_type call above */
2420 assert(0);
2421 /* fall through */
2422 case TYPE_STRUCT:
2423 case TYPE_UNION:
2424 case TYPE_ENCAPSULATED_UNION:
2425 case TYPE_ARRAY:
2426 case TYPE_POINTER:
2427 case TYPE_VOID:
2428 case TYPE_MODULE:
2429 case TYPE_COCLASS:
2430 case TYPE_FUNCTION:
2431 case TYPE_INTERFACE:
2432 case TYPE_BITFIELD:
2433 return FALSE;
2435 return FALSE;
2438 static int is_ptr_guid_type(const type_t *type)
2440 /* first, make sure it is a pointer to something */
2441 if (!is_ptr(type)) return FALSE;
2443 /* second, make sure it is a pointer to something of size sizeof(GUID),
2444 * i.e. 16 bytes */
2445 return (type_memsize(type_pointer_get_ref(type)) == 16);
2448 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2450 expr_t *dim;
2451 struct expr_loc expr_loc;
2452 expr_loc.v = arg;
2453 expr_loc.attr = attr_name;
2454 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2456 if (dim->type != EXPR_VOID)
2458 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2459 if (!is_allowed_conf_type(expr_type))
2460 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2461 attr_name);
2466 static void check_remoting_fields(const var_t *var, type_t *type);
2468 /* checks that properties common to fields and arguments are consistent */
2469 static void check_field_common(const type_t *container_type,
2470 const char *container_name, const var_t *arg)
2472 type_t *type = arg->type;
2473 int more_to_do;
2474 const char *container_type_name;
2475 const char *var_type;
2477 switch (type_get_type(container_type))
2479 case TYPE_STRUCT:
2480 container_type_name = "struct";
2481 var_type = "field";
2482 break;
2483 case TYPE_UNION:
2484 container_type_name = "union";
2485 var_type = "arm";
2486 break;
2487 case TYPE_ENCAPSULATED_UNION:
2488 container_type_name = "encapsulated union";
2489 var_type = "arm";
2490 break;
2491 case TYPE_FUNCTION:
2492 container_type_name = "function";
2493 var_type = "parameter";
2494 break;
2495 default:
2496 /* should be no other container types */
2497 assert(0);
2498 return;
2501 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2502 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2503 error_loc_info(&arg->loc_info,
2504 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2505 arg->name);
2507 if (is_attr(arg->attrs, ATTR_SIZEIS))
2509 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2510 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2512 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2514 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2515 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2517 if (is_attr(arg->attrs, ATTR_IIDIS))
2519 struct expr_loc expr_loc;
2520 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2521 if (expr->type != EXPR_VOID)
2523 const type_t *expr_type;
2524 expr_loc.v = arg;
2525 expr_loc.attr = "iid_is";
2526 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2527 if (!expr_type || !is_ptr_guid_type(expr_type))
2528 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2531 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2533 struct expr_loc expr_loc;
2534 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2535 if (expr->type != EXPR_VOID)
2537 const type_t *expr_type;
2538 expr_loc.v = arg;
2539 expr_loc.attr = "switch_is";
2540 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2541 if (!expr_type || !is_allowed_conf_type(expr_type))
2542 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2543 expr_loc.attr);
2549 more_to_do = FALSE;
2551 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
2553 case TGT_STRUCT:
2554 case TGT_UNION:
2555 check_remoting_fields(arg, type);
2556 break;
2557 case TGT_INVALID:
2559 const char *reason = "is invalid";
2560 switch (type_get_type(type))
2562 case TYPE_VOID:
2563 reason = "cannot derive from void *";
2564 break;
2565 case TYPE_FUNCTION:
2566 reason = "cannot be a function pointer";
2567 break;
2568 case TYPE_BITFIELD:
2569 reason = "cannot be a bit-field";
2570 break;
2571 case TYPE_COCLASS:
2572 reason = "cannot be a class";
2573 break;
2574 case TYPE_INTERFACE:
2575 reason = "cannot be a non-pointer to an interface";
2576 break;
2577 case TYPE_MODULE:
2578 reason = "cannot be a module";
2579 break;
2580 default:
2581 break;
2583 error_loc_info(&arg->loc_info, "%s \'%s\' of %s \'%s\' %s\n",
2584 var_type, arg->name, container_type_name, container_name, reason);
2585 break;
2587 case TGT_CTXT_HANDLE:
2588 case TGT_CTXT_HANDLE_POINTER:
2589 if (type_get_type(container_type) != TYPE_FUNCTION)
2590 error_loc_info(&arg->loc_info,
2591 "%s \'%s\' of %s \'%s\' cannot be a context handle\n",
2592 var_type, arg->name, container_type_name,
2593 container_name);
2594 break;
2595 case TGT_STRING:
2597 const type_t *t = type;
2598 while (is_ptr(t))
2599 t = type_pointer_get_ref(t);
2600 if (is_aliaschain_attr(t, ATTR_RANGE))
2601 warning_loc_info(&arg->loc_info, "%s: range not verified for a string of ranged types\n", arg->name);
2602 break;
2604 case TGT_POINTER:
2605 type = type_pointer_get_ref(type);
2606 more_to_do = TRUE;
2607 break;
2608 case TGT_ARRAY:
2609 type = type_array_get_element(type);
2610 more_to_do = TRUE;
2611 break;
2612 case TGT_USER_TYPE:
2613 case TGT_IFACE_POINTER:
2614 case TGT_BASIC:
2615 case TGT_ENUM:
2616 case TGT_RANGE:
2617 /* nothing to do */
2618 break;
2620 } while (more_to_do);
2623 static void check_remoting_fields(const var_t *var, type_t *type)
2625 const var_t *field;
2626 const var_list_t *fields = NULL;
2628 type = type_get_real_type(type);
2630 if (type->checked)
2631 return;
2633 type->checked = TRUE;
2635 if (type_get_type(type) == TYPE_STRUCT)
2637 if (type_is_complete(type))
2638 fields = type_struct_get_fields(type);
2639 else
2640 error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2642 else if (type_get_type(type) == TYPE_UNION || type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2643 fields = type_union_get_cases(type);
2645 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2646 if (field->type) check_field_common(type, type->name, field);
2649 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2650 static void check_remoting_args(const var_t *func)
2652 const char *funcname = func->name;
2653 const var_t *arg;
2655 if (func->type->details.function->args) LIST_FOR_EACH_ENTRY( arg, func->type->details.function->args, const var_t, entry )
2657 const type_t *type = arg->type;
2659 /* check that [out] parameters have enough pointer levels */
2660 if (is_attr(arg->attrs, ATTR_OUT))
2662 switch (typegen_detect_type(type, arg->attrs, TDT_ALL_TYPES))
2664 case TGT_BASIC:
2665 case TGT_ENUM:
2666 case TGT_RANGE:
2667 case TGT_STRUCT:
2668 case TGT_UNION:
2669 case TGT_CTXT_HANDLE:
2670 case TGT_USER_TYPE:
2671 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2672 break;
2673 case TGT_IFACE_POINTER:
2674 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2675 break;
2676 case TGT_STRING:
2677 if (is_array(type))
2679 /* needs conformance or fixed dimension */
2680 if (type_array_has_conformance(type) &&
2681 type_array_get_conformance(type)->type != EXPR_VOID) break;
2682 if (!type_array_has_conformance(type) && type_array_get_dim(type)) break;
2684 if (is_attr( arg->attrs, ATTR_IN )) break;
2685 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' cannot be an unsized string\n", arg->name, funcname);
2686 break;
2687 case TGT_INVALID:
2688 /* already error'd before we get here */
2689 case TGT_CTXT_HANDLE_POINTER:
2690 case TGT_POINTER:
2691 case TGT_ARRAY:
2692 /* OK */
2693 break;
2697 check_field_common(func->type, funcname, arg);
2700 if (type_get_type(type_function_get_rettype(func->type)) != TYPE_VOID)
2702 var_t var;
2703 var = *func;
2704 var.type = type_function_get_rettype(func->type);
2705 var.name = xstrdup("return value");
2706 check_field_common(func->type, funcname, &var);
2707 free(var.name);
2711 static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func)
2713 unsigned char explicit_fc, implicit_fc;
2715 /* check for a defined binding handle */
2716 if (!get_func_handle_var( iface, func, &explicit_fc, &implicit_fc ) || !explicit_fc)
2718 /* no explicit handle specified so add
2719 * "[in] handle_t IDL_handle" as the first parameter to the
2720 * function */
2721 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2722 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2723 idl_handle->type = find_type_or_error("handle_t", 0);
2724 type_function_add_head_arg(func->type, idl_handle);
2728 static void check_functions(const type_t *iface, int is_inside_library)
2730 const statement_t *stmt;
2731 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
2733 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2735 var_t *func = stmt->u.var;
2736 add_explicit_handle_if_necessary(iface, func);
2739 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2741 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2743 const var_t *func = stmt->u.var;
2744 if (!is_attr(func->attrs, ATTR_LOCAL))
2745 check_remoting_args(func);
2750 static void check_statements(const statement_list_t *stmts, int is_inside_library)
2752 const statement_t *stmt;
2754 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2756 switch(stmt->type) {
2757 case STMT_LIBRARY:
2758 check_statements(stmt->u.lib->stmts, TRUE);
2759 break;
2760 case STMT_TYPE:
2761 switch(type_get_type(stmt->u.type)) {
2762 case TYPE_INTERFACE:
2763 check_functions(stmt->u.type, is_inside_library);
2764 break;
2765 case TYPE_COCLASS:
2766 if(winrt_mode)
2767 error_loc("coclass is not allowed in Windows Runtime mode\n");
2768 break;
2769 default:
2770 break;
2772 break;
2773 default:
2774 break;
2779 static void check_all_user_types(const statement_list_t *stmts)
2781 const statement_t *stmt;
2783 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2785 if (stmt->type == STMT_LIBRARY)
2786 check_all_user_types(stmt->u.lib->stmts);
2787 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE &&
2788 !is_local(stmt->u.type->attrs))
2790 const statement_t *stmt_func;
2791 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type)) {
2792 const var_t *func = stmt_func->u.var;
2793 check_for_additional_prototype_types(func->type->details.function->args);
2799 int is_valid_uuid(const char *s)
2801 int i;
2803 for (i = 0; i < 36; ++i)
2804 if (i == 8 || i == 13 || i == 18 || i == 23)
2806 if (s[i] != '-')
2807 return FALSE;
2809 else
2810 if (!isxdigit(s[i]))
2811 return FALSE;
2813 return s[i] == '\0';
2816 static statement_t *make_statement(enum statement_type type)
2818 statement_t *stmt = xmalloc(sizeof(*stmt));
2819 stmt->type = type;
2820 return stmt;
2823 static statement_t *make_statement_type_decl(type_t *type)
2825 statement_t *stmt = make_statement(STMT_TYPE);
2826 stmt->u.type = type;
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->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->type) == TYPE_FUNCTION)
2849 check_function_attrs(var->name, var->attrs);
2850 else if (var->stgclass == STG_NONE || var->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)
2899 declarator_t *decl, *next;
2900 statement_t *stmt;
2901 type_list_t **type_list;
2903 if (!decls) return NULL;
2905 stmt = make_statement(STMT_TYPEDEF);
2906 stmt->u.type_list = NULL;
2907 type_list = &stmt->u.type_list;
2909 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2911 var_t *var = decl->var;
2912 type_t *type = find_type_or_error(var->name, 0);
2913 *type_list = xmalloc(sizeof(type_list_t));
2914 (*type_list)->type = type;
2915 (*type_list)->next = NULL;
2917 type_list = &(*type_list)->next;
2918 free(decl);
2919 free(var);
2922 return stmt;
2925 static statement_list_t *append_statements(statement_list_t *l1, statement_list_t *l2)
2927 if (!l2) return l1;
2928 if (!l1 || l1 == l2) return l2;
2929 list_move_tail (l1, l2);
2930 return l1;
2933 static attr_list_t *append_attribs(attr_list_t *l1, attr_list_t *l2)
2935 if (!l2) return l1;
2936 if (!l1 || l1 == l2) return l2;
2937 list_move_tail (l1, l2);
2938 return l1;
2941 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2943 if (!stmt) return list;
2944 if (!list)
2946 list = xmalloc( sizeof(*list) );
2947 list_init( list );
2949 list_add_tail( list, &stmt->entry );
2950 return list;
2953 void init_loc_info(loc_info_t *i)
2955 i->input_name = input_name ? input_name : "stdin";
2956 i->line_number = line_number;
2957 i->near_text = parser_text;
2960 static void check_def(const type_t *t)
2962 if (t->defined)
2963 error_loc("%s: redefinition error; original definition was at %s:%d\n",
2964 t->name, t->loc_info.input_name, t->loc_info.line_number);