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