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