5 * Copyright 2002 Ove Kaaven
6 * Copyright 2006-2008 Robert Shearman
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
45 /* Berkeley yacc (byacc) doesn't seem to know about these */
46 /* Some *BSD supplied versions do define these though */
48 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
51 # define YYLEX yylex()
54 #elif defined(YYBISON)
55 /* Bison was used for original development */
56 /* #define YYEMPTY -2 */
57 /* #define YYLEX yylex() */
60 /* No yacc we know yet */
61 # if !defined(YYEMPTY) || !defined(YYLEX)
62 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
63 # elif defined(__GNUC__) /* gcc defines the #warning directive */
64 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
65 /* #else we just take a chance that it works... */
69 #define YYERROR_VERBOSE
71 unsigned char pointer_default = RPC_FC_UP;
72 static int is_in_interface = FALSE;
73 static int is_object_interface = FALSE;
74 /* are we inside a library block? */
75 static int is_inside_library = FALSE;
77 typedef struct list typelist_t;
89 typedef struct _decl_spec_t
93 enum storage_class stgclass;
96 typelist_t incomplete_types = LIST_INIT(incomplete_types);
98 static void add_incomplete(type_t *t);
99 static void fix_incomplete(void);
101 static str_list_t *append_str(str_list_t *list, char *str);
102 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
103 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
104 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);
105 static attr_t *make_attr(enum attr_type type);
106 static attr_t *make_attrv(enum attr_type type, unsigned long val);
107 static attr_t *make_attrp(enum attr_type type, void *val);
108 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
109 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
110 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl, int top);
111 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
112 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
113 static ifref_t *make_ifref(type_t *iface);
114 static var_list_t *append_var(var_list_t *list, var_t *var);
115 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
116 static var_t *make_var(char *name);
117 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
118 static declarator_t *make_declarator(var_t *var);
119 static func_list_t *append_func(func_list_t *list, func_t *func);
120 static func_t *make_func(var_t *def);
121 static type_t *make_class(char *name);
122 static type_t *make_safearray(type_t *type);
123 static type_t *make_builtin(char *name);
124 static type_t *make_int(int sign);
125 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
126 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
128 static type_t *type_new_enum(var_t *name, var_list_t *enums);
129 static type_t *type_new_struct(var_t *name, var_list_t *fields);
130 static type_t *type_new_nonencapsulated_union(var_t *name, var_list_t *fields);
131 static type_t *type_new_encapsulated_union(var_t *name, var_t *switch_field, var_t *union_field, var_list_t *cases);
133 static type_t *reg_type(type_t *type, const char *name, int t);
134 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
135 static type_t *find_type_or_error(const char *name, int t);
136 static type_t *find_type_or_error2(char *name, int t);
137 static type_t *get_type(unsigned char type, char *name, int t);
138 static type_t *get_typev(unsigned char type, var_t *name, int t);
140 static var_t *reg_const(var_t *var);
142 static void write_libid(const typelib_t *typelib);
143 static void write_clsid(type_t *cls);
144 static void write_diid(type_t *iface);
145 static void write_iid(type_t *iface);
147 static char *gen_name(void);
148 static statement_t *process_typedefs(var_list_t *names);
149 static void check_arg(var_t *arg);
150 static void check_all_user_types(const statement_list_t *stmts);
151 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
152 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
153 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
154 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
155 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
156 static attr_list_t *check_union_attrs(attr_list_t *attrs);
157 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
158 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
159 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
160 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
161 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
162 const char *get_attr_display_name(enum attr_type type);
163 static void add_explicit_handle_if_necessary(func_t *func);
164 static void check_def(const type_t *t);
166 static statement_t *make_statement(enum statement_type type);
167 static statement_t *make_statement_type_decl(type_t *type);
168 static statement_t *make_statement_reference(type_t *type);
169 static statement_t *make_statement_declaration(var_t *var);
170 static statement_t *make_statement_library(typelib_t *typelib);
171 static statement_t *make_statement_cppquote(const char *str);
172 static statement_t *make_statement_importlib(const char *str);
173 static statement_t *make_statement_module(type_t *type);
174 static statement_t *make_statement_import(const char *str);
175 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
176 static func_list_t *append_func_from_statement(func_list_t *list, statement_t *stmt);
185 attr_list_t *attr_list;
186 str_list_t *str_list;
188 expr_list_t *expr_list;
189 array_dims_t *array_dims;
192 var_list_t *var_list;
193 declarator_t *declarator;
194 declarator_list_t *declarator_list;
196 func_list_t *func_list;
197 statement_t *statement;
198 statement_list_t *stmt_list;
200 ifref_list_t *ifref_list;
205 interface_info_t ifinfo;
207 struct _import_t *import;
208 struct _decl_spec_t *declspec;
209 enum storage_class stgclass;
212 %token <str> aIDENTIFIER
213 %token <str> aKNOWNTYPE
214 %token <num> aNUM aHEXNUM
216 %token <str> aSTRING aWSTRING
221 %token EQUALITY INEQUALITY
222 %token GREATEREQUAL LESSEQUAL
223 %token LOGICALOR LOGICALAND
224 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
225 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
226 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
227 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
228 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
230 %token tDEFAULTCOLLELEM
232 %token tDEFAULTVTABLE
234 %token tDISPINTERFACE
235 %token tDLLNAME tDOUBLE tDUAL
237 %token tENTRY tENUM tERRORSTATUST
238 %token tEXPLICITHANDLE tEXTERN
244 %token tHELPCONTEXT tHELPFILE
245 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
247 %token tHYPER tID tIDEMPOTENT
249 %token tIMMEDIATEBIND
250 %token tIMPLICITHANDLE
251 %token tIMPORT tIMPORTLIB
252 %token tIN tIN_LINE tINLINE
257 %token tLENGTHIS tLIBRARY
264 %token tNONEXTENSIBLE
266 %token tOBJECT tODL tOLEAUTOMATION
270 %token tPOINTERDEFAULT
272 %token tPROPGET tPROPPUT tPROPPUTREF
276 %token tREADONLY tREF
285 %token tSIZEIS tSIZEOF
290 %token tSTRICTCONTEXTHANDLE
291 %token tSTRING tSTRUCT
292 %token tSWITCH tSWITCHIS tSWITCHTYPE
304 %token tWCHAR tWIREMARSHAL
306 %type <attr> attribute type_qualifier function_specifier
307 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
308 %type <str_list> str_list
309 %type <expr> m_expr expr expr_const expr_int_const array
310 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
311 %type <ifinfo> interfacehdr
312 %type <stgclass> storage_cls_spec
313 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
314 %type <type> inherit interface interfacedef interfacedec
315 %type <type> dispinterface dispinterfacehdr dispinterfacedef
316 %type <type> module modulehdr moduledef
317 %type <type> base_type int_std
318 %type <type> enumdef structdef uniondef typedecl
320 %type <ifref> coclass_int
321 %type <ifref_list> coclass_ints
322 %type <var> arg ne_union_field union_field s_field case enum declaration
323 %type <var_list> m_args no_args args fields ne_union_fields cases enums enum_list dispint_props field
324 %type <var> m_ident t_ident ident
325 %type <declarator> declarator direct_declarator init_declarator
326 %type <declarator_list> declarator_list
328 %type <func_list> int_statements dispint_meths
329 %type <type> coclass coclasshdr coclassdef
330 %type <num> pointer_type version
331 %type <str> libraryhdr callconv cppquote importlib import
332 %type <uuid> uuid_string
333 %type <import> import_start
334 %type <typelib> library_start librarydef
335 %type <statement> statement typedef
336 %type <stmt_list> gbl_statements imp_statements
345 %left EQUALITY INEQUALITY
346 %left '<' '>' LESSEQUAL GREATEREQUAL
350 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
351 %left '.' MEMBERPTR '[' ']'
355 input: gbl_statements { fix_incomplete();
356 check_all_user_types($1);
364 gbl_statements: { $$ = NULL; }
365 | gbl_statements interfacedec { $$ = $1; }
366 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
367 | gbl_statements coclass ';' { $$ = $1;
368 reg_type($2, $2->name, 0);
369 if (!parse_only && do_header) write_coclass_forward($2);
371 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
372 add_typelib_entry($2);
373 reg_type($2, $2->name, 0);
374 if (!parse_only && do_header) write_coclass_forward($2);
376 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2));
377 add_typelib_entry($2);
379 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
380 | gbl_statements statement { $$ = append_statement($1, $2); }
383 imp_statements: { $$ = NULL; }
384 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); if (!parse_only) add_typelib_entry($2); }
385 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); if (!parse_only) add_typelib_entry($2); }
386 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
387 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
388 if (!parse_only) add_typelib_entry($2);
389 reg_type($2, $2->name, 0);
390 if (!parse_only && do_header) write_coclass_forward($2);
392 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); if (!parse_only) add_typelib_entry($2); }
393 | imp_statements statement { $$ = append_statement($1, $2); }
394 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
395 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
398 int_statements: { $$ = NULL; }
399 | int_statements statement { $$ = append_func_from_statement( $1, $2 ); }
407 cppquote { $$ = make_statement_cppquote($1); }
408 | typedecl ';' { $$ = make_statement_type_decl($1);
409 if (!parse_only && do_header) {
410 write_type_def_or_decl(header, $1, FALSE, NULL);
411 fprintf(header, ";\n\n");
414 | declaration ';' { $$ = make_statement_declaration($1);
415 if (!parse_only && do_header) write_declaration($1, is_in_interface);
417 | import { $$ = make_statement_import($1); }
418 | typedef ';' { $$ = $1; }
425 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
426 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
427 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
430 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
432 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
433 $$ = xmalloc(sizeof(struct _import_t));
435 $$->import_performed = do_import($2);
436 if (!$$->import_performed) yychar = aEOF;
440 import: import_start imp_statements aEOF { $$ = $1->name;
441 if ($1->import_performed) pop_import();
443 if (!parse_only && do_header) write_import($$);
447 importlib: tIMPORTLIB '(' aSTRING ')'
448 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
451 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
453 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
454 if (!parse_only) start_typelib($$);
455 if (!parse_only && do_header) write_library($$);
456 if (!parse_only && do_idfile) write_libid($$);
457 is_inside_library = TRUE;
460 librarydef: library_start imp_statements '}'
461 semicolon_opt { $$ = $1;
463 if (!parse_only) end_typelib();
464 is_inside_library = FALSE;
468 m_args: { $$ = NULL; }
472 no_args: tVOID { $$ = NULL; }
475 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
476 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
480 /* split into two rules to get bison to resolve a tVOID conflict */
481 arg: attributes decl_spec declarator { $$ = $3->var;
483 if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
484 error_loc("invalid storage class for function parameter\n");
485 set_type($$, $2, $3, TRUE);
488 | decl_spec declarator { $$ = $2->var;
489 if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
490 error_loc("invalid storage class for function parameter\n");
491 set_type($$, $1, $2, TRUE);
496 array: '[' m_expr ']' { $$ = $2; }
497 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
500 m_attributes: { $$ = NULL; }
505 '[' attrib_list ']' { $$ = $2; }
508 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
509 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
510 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
513 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
514 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
517 attribute: { $$ = NULL; }
518 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
519 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
520 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
521 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
522 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
523 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
524 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
525 | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
526 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
527 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
528 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
529 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
530 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
531 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
532 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
533 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
534 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
535 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
536 | tDUAL { $$ = make_attr(ATTR_DUAL); }
537 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
538 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
539 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
540 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
541 | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
542 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
543 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
544 | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
545 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
546 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
547 | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
548 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
549 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
550 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
551 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
552 | tIN { $$ = make_attr(ATTR_IN); }
553 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
554 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
555 | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
556 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
557 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
558 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
559 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
560 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
561 | tODL { $$ = make_attr(ATTR_ODL); }
562 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
563 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
564 | tOUT { $$ = make_attr(ATTR_OUT); }
565 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
566 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
567 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
568 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
569 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
570 | tRANGE '(' expr_int_const ',' expr_int_const ')'
571 { expr_list_t *list = append_expr( NULL, $3 );
572 list = append_expr( list, $5 );
573 $$ = make_attrp(ATTR_RANGE, list); }
574 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
575 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
576 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
577 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
578 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
579 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
580 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
581 | tSTRING { $$ = make_attr(ATTR_STRING); }
582 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
583 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
584 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
585 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
586 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
587 | tVARARG { $$ = make_attr(ATTR_VARARG); }
588 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
589 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
590 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
595 | aSTRING { if (!is_valid_uuid($1))
596 error_loc("invalid UUID: %s\n", $1);
597 $$ = parse_uuid($1); }
600 callconv: tCDECL { $$ = $<str>1; }
601 | tFASTCALL { $$ = $<str>1; }
602 | tPASCAL { $$ = $<str>1; }
603 | tSTDCALL { $$ = $<str>1; }
606 cases: { $$ = NULL; }
607 | cases case { $$ = append_var( $1, $2 ); }
610 case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
611 $$ = $4; if (!$$) $$ = make_var(NULL);
612 $$->attrs = append_attr( $$->attrs, a );
614 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
615 $$ = $3; if (!$$) $$ = make_var(NULL);
616 $$->attrs = append_attr( $$->attrs, a );
620 enums: { $$ = NULL; }
621 | enum_list ',' { $$ = $1; }
625 enum_list: enum { if (!$1->eval)
626 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
627 $$ = append_var( NULL, $1 );
629 | enum_list ',' enum { if (!$3->eval)
631 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
632 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
634 $$ = append_var( $1, $3 );
638 enum: ident '=' expr_int_const { $$ = reg_const($1);
640 $$->type = make_int(0);
642 | ident { $$ = reg_const($1);
643 $$->type = make_int(0);
647 enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, $4);
649 add_typelib_entry($$);
653 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
654 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
658 exprs: { $$ = make_expr(EXPR_VOID); }
663 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
667 m_expr: { $$ = make_expr(EXPR_VOID); }
671 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
672 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
673 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
674 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
675 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
676 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
677 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
678 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
679 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
680 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
681 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
682 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
683 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
684 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
685 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
686 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
687 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
688 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
689 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
690 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
691 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
692 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
693 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
694 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
695 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
696 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
697 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
698 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
699 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
700 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
701 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
702 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
703 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
704 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
705 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
706 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
707 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
708 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
709 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
710 | '(' expr ')' { $$ = $2; }
713 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
714 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
717 expr_int_const: expr { $$ = $1;
719 error_loc("expression is not an integer constant\n");
723 expr_const: expr { $$ = $1;
724 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
725 error_loc("expression is not constant\n");
729 fields: { $$ = NULL; }
730 | fields field { $$ = append_var_list($1, $2); }
733 field: m_attributes decl_spec declarator_list ';'
734 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
735 check_field_attrs(first, $1);
736 $$ = set_var_types($1, $2, $3);
738 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
739 v->type = $2; v->attrs = $1;
740 $$ = append_var(NULL, v);
745 s_field ';' { $$ = $1; }
746 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
749 ne_union_fields: { $$ = NULL; }
750 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
754 s_field ';' { $$ = $1; }
758 s_field: m_attributes decl_spec declarator { $$ = $3->var;
759 $$->attrs = check_field_attrs($$->name, $1);
760 set_type($$, $2, $3, FALSE);
766 m_attributes decl_spec declarator { var_t *v = $3->var;
767 v->attrs = check_function_attrs(v->name, $1);
768 set_type(v, $2, $3, FALSE);
775 attributes decl_spec init_declarator
778 set_type($$, $2, $3, FALSE);
781 | decl_spec init_declarator { $$ = $2->var;
782 set_type($$, $1, $2, FALSE);
787 m_ident: { $$ = NULL; }
791 t_ident: { $$ = NULL; }
792 | aIDENTIFIER { $$ = make_var($1); }
793 | aKNOWNTYPE { $$ = make_var($1); }
796 ident: aIDENTIFIER { $$ = make_var($1); }
797 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
798 | aKNOWNTYPE { $$ = make_var($<str>1); }
801 base_type: tBYTE { $$ = make_builtin($<str>1); }
802 | tWCHAR { $$ = make_builtin($<str>1); }
804 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
805 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
807 case RPC_FC_CHAR: break;
808 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
809 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
810 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
812 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
814 $$ = alias($$, "MIDL_uhyper");
821 | tUNSIGNED { $$ = make_int(-1); }
822 | tFLOAT { $$ = make_builtin($<str>1); }
823 | tSINGLE { $$ = find_type("float", 0); }
824 | tDOUBLE { $$ = make_builtin($<str>1); }
825 | tBOOLEAN { $$ = make_builtin($<str>1); }
826 | tERRORSTATUST { $$ = make_builtin($<str>1); }
827 | tHANDLET { $$ = make_builtin($<str>1); }
834 int_std: tINT { $$ = make_builtin($<str>1); }
835 | tSHORT m_int { $$ = make_builtin($<str>1); }
836 | tSMALL { $$ = make_builtin($<str>1); }
837 | tLONG m_int { $$ = make_builtin($<str>1); }
838 | tHYPER m_int { $$ = make_builtin($<str>1); }
839 | tINT64 { $$ = make_builtin($<str>1); }
840 | tCHAR { $$ = make_builtin($<str>1); }
843 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
844 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
845 if ($$->kind != TKIND_COCLASS)
846 error_loc("%s was not declared a coclass at %s:%d\n",
847 $2, $$->loc_info.input_name,
848 $$->loc_info.line_number);
852 coclasshdr: attributes coclass { $$ = $2;
854 $$->attrs = check_coclass_attrs($2->name, $1);
855 if (!parse_only && do_header)
857 if (!parse_only && do_idfile)
862 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
869 coclass_ints: { $$ = NULL; }
870 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
874 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
877 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
878 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
881 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
882 is_in_interface = TRUE;
883 is_object_interface = TRUE;
886 attrs = make_attr(ATTR_DISPINTERFACE);
887 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
889 if (!parse_only && do_header) write_forward($$);
893 dispint_props: tPROPERTIES ':' { $$ = NULL; }
894 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
897 dispint_meths: tMETHODS ':' { $$ = NULL; }
898 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
901 dispinterfacedef: dispinterfacehdr '{'
905 type_dispinterface_define($$, $3, $4);
906 if (!parse_only && do_header) write_interface($$);
907 if (!parse_only && do_idfile) write_diid($$);
908 is_in_interface = FALSE;
911 '{' interface ';' '}' { $$ = $1;
912 type_dispinterface_define_from_iface($$, $3);
913 if (!parse_only && do_header) write_interface($$);
914 if (!parse_only && do_idfile) write_diid($$);
915 is_in_interface = FALSE;
919 inherit: { $$ = NULL; }
920 | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
923 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
924 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
927 interfacehdr: attributes interface { $$.interface = $2;
928 $$.old_pointer_default = pointer_default;
929 if (is_attr($1, ATTR_POINTERDEFAULT))
930 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
931 is_object_interface = is_object($1);
932 is_in_interface = TRUE;
934 $2->attrs = check_iface_attrs($2->name, $1);
936 if (!parse_only && do_header) write_forward($2);
940 interfacedef: interfacehdr inherit
941 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
942 type_interface_define($$, $2, $4);
943 if (!parse_only && do_header) write_interface($$);
944 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
945 if (!parse_only && do_idfile) write_iid($$);
946 pointer_default = $1.old_pointer_default;
947 is_in_interface = FALSE;
949 /* MIDL is able to import the definition of a base class from inside the
950 * definition of a derived class, I'll try to support it with this rule */
951 | interfacehdr ':' aIDENTIFIER
952 '{' import int_statements '}'
953 semicolon_opt { $$ = $1.interface;
954 type_interface_define($$, find_type_or_error2($3, 0), $6);
955 if (!parse_only && do_header) write_interface($$);
956 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
957 if (!parse_only && do_idfile) write_iid($$);
958 pointer_default = $1.old_pointer_default;
959 is_in_interface = FALSE;
961 | dispinterfacedef semicolon_opt { $$ = $1; }
965 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
966 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
969 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
970 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
973 modulehdr: attributes module { $$ = $2;
974 $$->attrs = check_module_attrs($2->name, $1);
978 moduledef: modulehdr '{' int_statements '}'
979 semicolon_opt { $$ = $1;
981 /* FIXME: if (!parse_only && do_header) write_module($$); */
986 tEXTERN { $$ = STG_EXTERN; }
987 | tSTATIC { $$ = STG_STATIC; }
988 | tREGISTER { $$ = STG_REGISTER; }
992 tINLINE { $$ = make_attr(ATTR_INLINE); }
996 tCONST { $$ = make_attr(ATTR_CONST); }
999 m_type_qual_list: { $$ = NULL; }
1000 | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
1003 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
1004 | decl_spec_no_type type m_decl_spec_no_type
1005 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
1008 m_decl_spec_no_type: { $$ = NULL; }
1013 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
1014 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
1015 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
1019 '*' m_type_qual_list declarator %prec PPTR
1020 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(NULL, $2)); }
1021 | callconv declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
1026 ident { $$ = make_declarator($1); }
1027 | '(' declarator ')' { $$ = $2; }
1028 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
1029 | direct_declarator '(' m_args ')' { $$ = $1;
1030 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
1036 declarator { $$ = append_declarator( NULL, $1 ); }
1037 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
1041 declarator { $$ = $1; }
1042 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
1046 tREF { $$ = RPC_FC_RP; }
1047 | tUNIQUE { $$ = RPC_FC_UP; }
1048 | tPTR { $$ = RPC_FC_FP; }
1051 structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, $4);
1053 add_typelib_entry($$);
1057 type: tVOID { $$ = find_type_or_error("void", 0); }
1058 | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
1059 | base_type { $$ = $1; }
1060 | enumdef { $$ = $1; }
1061 | tENUM aIDENTIFIER { $$ = find_type_or_error2($2, tsENUM); }
1062 | structdef { $$ = $1; }
1063 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
1064 | uniondef { $$ = $1; }
1065 | tUNION aIDENTIFIER { $$ = find_type_or_error2($2, tsUNION); }
1066 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
1069 typedef: tTYPEDEF m_attributes decl_spec declarator_list
1070 { reg_typedefs($3, $4, check_typedef_attrs($2));
1071 $$ = process_typedefs($4);
1075 uniondef: tUNION t_ident '{' ne_union_fields '}'
1076 { $$ = type_new_nonencapsulated_union($2, $4); }
1078 tSWITCH '(' s_field ')'
1079 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1083 aNUM { $$ = MAKEVERSION($1, 0); }
1084 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1089 static void decl_builtin(const char *name, unsigned char type)
1091 type_t *t = make_type(type, NULL);
1092 t->name = xstrdup(name);
1093 reg_type(t, name, 0);
1096 static type_t *make_builtin(char *name)
1098 /* NAME is strdup'd in the lexer */
1099 type_t *t = duptype(find_type_or_error(name, 0), 0);
1104 static type_t *make_int(int sign)
1106 type_t *t = duptype(find_type_or_error("int", 0), 1);
1110 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
1115 void init_types(void)
1117 decl_builtin("void", 0);
1118 decl_builtin("byte", RPC_FC_BYTE);
1119 decl_builtin("wchar_t", RPC_FC_WCHAR);
1120 decl_builtin("int", RPC_FC_LONG); /* win32 */
1121 decl_builtin("short", RPC_FC_SHORT);
1122 decl_builtin("small", RPC_FC_SMALL);
1123 decl_builtin("long", RPC_FC_LONG);
1124 decl_builtin("hyper", RPC_FC_HYPER);
1125 decl_builtin("__int64", RPC_FC_HYPER);
1126 decl_builtin("char", RPC_FC_CHAR);
1127 decl_builtin("float", RPC_FC_FLOAT);
1128 decl_builtin("double", RPC_FC_DOUBLE);
1129 decl_builtin("boolean", RPC_FC_BYTE);
1130 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1131 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1134 static str_list_t *append_str(str_list_t *list, char *str)
1136 struct str_list_entry_t *entry;
1138 if (!str) return list;
1141 list = xmalloc( sizeof(*list) );
1144 entry = xmalloc( sizeof(*entry) );
1146 list_add_tail( list, &entry->entry );
1150 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1152 attr_t *attr_existing;
1153 if (!attr) return list;
1156 list = xmalloc( sizeof(*list) );
1159 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1160 if (attr_existing->type == attr->type)
1162 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1163 /* use the last attribute, like MIDL does */
1164 list_remove(&attr_existing->entry);
1167 list_add_tail( list, &attr->entry );
1171 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1174 if (!src) return dst;
1175 LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1176 if (attr->type == type)
1178 list_remove(&attr->entry);
1179 return append_attr(dst, attr);
1184 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1188 if (!old_list) return new_list;
1190 while ((entry = list_head(old_list)))
1192 attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1194 new_list = append_attr(new_list, attr);
1199 static attr_list_t *dupattrs(const attr_list_t *list)
1201 attr_list_t *new_list;
1204 if (!list) return NULL;
1206 new_list = xmalloc( sizeof(*list) );
1207 list_init( new_list );
1208 LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1210 attr_t *new_attr = xmalloc(sizeof(*new_attr));
1212 list_add_tail(new_list, &new_attr->entry);
1217 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass)
1219 decl_spec_t *declspec = left ? left : right;
1222 declspec = xmalloc(sizeof(*declspec));
1223 declspec->type = NULL;
1224 declspec->attrs = NULL;
1225 declspec->stgclass = STG_NONE;
1227 declspec->type = type;
1228 if (left && declspec != left)
1230 declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1231 if (declspec->stgclass == STG_NONE)
1232 declspec->stgclass = left->stgclass;
1233 else if (left->stgclass != STG_NONE)
1234 error_loc("only one storage class can be specified\n");
1235 assert(!left->type);
1238 if (right && declspec != right)
1240 declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1241 if (declspec->stgclass == STG_NONE)
1242 declspec->stgclass = right->stgclass;
1243 else if (right->stgclass != STG_NONE)
1244 error_loc("only one storage class can be specified\n");
1245 assert(!right->type);
1249 declspec->attrs = append_attr(declspec->attrs, attr);
1250 if (declspec->stgclass == STG_NONE)
1251 declspec->stgclass = stgclass;
1252 else if (stgclass != STG_NONE)
1253 error_loc("only one storage class can be specified\n");
1255 /* apply attributes to type */
1256 if (type && declspec->attrs)
1259 declspec->type = duptype(type, 1);
1260 attrs = dupattrs(type->attrs);
1261 declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1262 declspec->attrs = NULL;
1268 static attr_t *make_attr(enum attr_type type)
1270 attr_t *a = xmalloc(sizeof(attr_t));
1276 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1278 attr_t *a = xmalloc(sizeof(attr_t));
1284 static attr_t *make_attrp(enum attr_type type, void *val)
1286 attr_t *a = xmalloc(sizeof(attr_t));
1292 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1294 if (!expr) return list;
1297 list = xmalloc( sizeof(*list) );
1300 list_add_tail( list, &expr->entry );
1304 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1306 if (!expr) return list;
1309 list = xmalloc( sizeof(*list) );
1312 list_add_tail( list, &expr->entry );
1316 static struct list type_pool = LIST_INIT(type_pool);
1323 type_t *alloc_type(void)
1325 type_pool_node_t *node = xmalloc(sizeof *node);
1326 list_add_tail(&type_pool, &node->link);
1330 void set_all_tfswrite(int val)
1332 type_pool_node_t *node;
1333 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1334 node->data.tfswrite = val;
1337 void clear_all_offsets(void)
1339 type_pool_node_t *node;
1340 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1341 node->data.typestring_offset = node->data.ptrdesc = 0;
1344 type_t *make_type(unsigned char type, type_t *ref)
1346 type_t *t = alloc_type();
1348 t->kind = TKIND_PRIMITIVE;
1354 t->fields_or_args = NULL;
1358 t->length_is = NULL;
1359 t->typestring_offset = 0;
1361 t->declarray = FALSE;
1362 t->ignore = (parse_only != 0);
1366 t->user_types_registered = FALSE;
1367 t->tfswrite = FALSE;
1369 t->typelib_idx = -1;
1370 init_loc_info(&t->loc_info);
1374 static type_t *type_new_enum(var_t *name, var_list_t *enums)
1376 type_t *t = get_typev(RPC_FC_ENUM16, name, tsENUM);
1377 t->kind = TKIND_ENUM;
1378 t->fields_or_args = enums;
1383 static type_t *type_new_struct(var_t *name, var_list_t *fields)
1385 type_t *t = get_typev(RPC_FC_STRUCT, name, tsSTRUCT);
1386 t->kind = TKIND_RECORD;
1387 t->fields_or_args = fields;
1392 static type_t *type_new_nonencapsulated_union(var_t *name, var_list_t *fields)
1394 type_t *t = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, name, tsUNION);
1395 t->kind = TKIND_UNION;
1396 t->fields_or_args = fields;
1401 static type_t *type_new_encapsulated_union(var_t *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
1403 type_t *t = get_typev(RPC_FC_ENCAPSULATED_UNION, name, tsUNION);
1404 t->kind = TKIND_UNION;
1405 if (!union_field) union_field = make_var( xstrdup("tagged_union") );
1406 union_field->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
1407 union_field->type->kind = TKIND_UNION;
1408 union_field->type->fields_or_args = cases;
1409 union_field->type->defined = TRUE;
1410 t->fields_or_args = append_var( NULL, switch_field );
1411 t->fields_or_args = append_var( t->fields_or_args, union_field );
1416 static void function_add_head_arg(func_t *func, var_t *arg)
1418 if (!func->def->type->fields_or_args)
1420 func->def->type->fields_or_args = xmalloc( sizeof(*func->def->type->fields_or_args) );
1421 list_init( func->def->type->fields_or_args );
1423 list_add_head( func->def->type->fields_or_args, &arg->entry );
1424 func->args = func->def->type->fields_or_args;
1427 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1429 type_t *ptrchain_type;
1432 for (ptrchain_type = ptrchain; ptrchain_type->ref; ptrchain_type = ptrchain_type->ref)
1434 ptrchain_type->ref = type;
1438 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl,
1441 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1442 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1443 int sizeless, has_varconf;
1445 type_t *atype, **ptype;
1446 array_dims_t *arr = decl ? decl->array : NULL;
1447 type_t *func_type = decl ? decl->func_type : NULL;
1448 type_t *type = decl_spec->type;
1450 if (is_attr(type->attrs, ATTR_INLINE))
1453 error_loc("inline attribute applied to non-function type\n");
1457 /* move inline attribute from return type node to function node */
1458 for (t = func_type; is_ptr(t); t = t->ref)
1460 t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1464 /* add type onto the end of the pointers in pident->type */
1465 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1466 v->stgclass = decl_spec->stgclass;
1468 /* the highest level of pointer specified should default to the var's ptr attr
1469 * or (RPC_FC_RP if not specified and it's a top level ptr), not
1470 * pointer_default so we need to fix that up here */
1473 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1474 const type_t *ptr = NULL;
1475 /* pointer attributes on the left side of the type belong to the function
1476 * pointer, if one is being declared */
1477 type_t **pt = func_type ? &func_type : &v->type;
1478 for (ptr = *pt; ptr && !ptr_attr; )
1480 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1481 if (!ptr_attr && ptr->kind == TKIND_ALIAS)
1486 if (ptr && is_ptr(ptr) && (ptr_attr || top))
1488 /* duplicate type to avoid changing original type */
1489 *pt = duptype(*pt, 1);
1490 (*pt)->type = ptr_attr ? ptr_attr : RPC_FC_RP;
1493 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1496 if (is_attr(v->attrs, ATTR_STRING) && !is_ptr(v->type) && !arr)
1497 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1500 if (is_attr(v->attrs, ATTR_V1ENUM))
1502 if (v->type->type == RPC_FC_ENUM16)
1503 v->type->type = RPC_FC_ENUM32;
1505 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1509 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1512 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1516 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1521 v->type = make_type(RPC_FC_CARRAY, v->type);
1524 v->type->declarray = TRUE;
1525 v->type->dim = dim->cval;
1529 has_varconf = FALSE;
1530 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1532 if (dim->type != EXPR_VOID)
1535 atype = *ptype = duptype(*ptype, 0);
1537 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1538 error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1540 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1541 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1543 atype->type = RPC_FC_CARRAY;
1544 atype->size_is = dim;
1547 ptype = &(*ptype)->ref;
1549 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1553 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1555 if (dim->type != EXPR_VOID)
1558 atype = *ptype = duptype(*ptype, 0);
1560 if (atype->type == RPC_FC_SMFARRAY)
1561 atype->type = RPC_FC_SMVARRAY;
1562 else if (atype->type == RPC_FC_LGFARRAY)
1563 atype->type = RPC_FC_LGVARRAY;
1564 else if (atype->type == RPC_FC_CARRAY)
1565 atype->type = RPC_FC_CVARRAY;
1567 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1569 atype->length_is = dim;
1572 ptype = &(*ptype)->ref;
1574 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1577 if (has_varconf && !last_array(v->type))
1580 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1582 *ptype = duptype(*ptype, 0);
1583 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1587 /* v->type is currently pointing to the type on the left-side of the
1588 * declaration, so we need to fix this up so that it is the return type of the
1589 * function and make v->type point to the function side of the declaration */
1593 type_t *return_type = v->type;
1594 v->type = func_type;
1595 for (ft = v->type; is_ptr(ft); ft = ft->ref)
1597 assert(ft->type == RPC_FC_FUNCTION);
1598 ft->ref = return_type;
1599 /* move calling convention attribute, if present, from pointer nodes to
1601 for (t = v->type; is_ptr(t); t = t->ref)
1602 ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1603 if (is_object_interface && !is_attr(ft->attrs, ATTR_CALLCONV))
1605 static char *stdmethodcalltype;
1606 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1607 ft->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1613 for (t = v->type; is_ptr(t); t = t->ref)
1614 if (is_attr(t->attrs, ATTR_CALLCONV))
1615 error_loc("calling convention applied to non-function-pointer type\n");
1619 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1621 declarator_t *decl, *next;
1622 var_list_t *var_list = NULL;
1624 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1626 var_t *var = decl->var;
1629 set_type(var, decl_spec, decl, 0);
1630 var_list = append_var(var_list, var);
1636 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1638 if (!iface) return list;
1641 list = xmalloc( sizeof(*list) );
1644 list_add_tail( list, &iface->entry );
1648 static ifref_t *make_ifref(type_t *iface)
1650 ifref_t *l = xmalloc(sizeof(ifref_t));
1656 static var_list_t *append_var(var_list_t *list, var_t *var)
1658 if (!var) return list;
1661 list = xmalloc( sizeof(*list) );
1664 list_add_tail( list, &var->entry );
1668 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1670 if (!vars) return list;
1673 list = xmalloc( sizeof(*list) );
1676 list_move_tail( list, vars );
1680 static var_t *make_var(char *name)
1682 var_t *v = xmalloc(sizeof(var_t));
1687 v->stgclass = STG_NONE;
1688 init_loc_info(&v->loc_info);
1692 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1694 if (!d) return list;
1696 list = xmalloc(sizeof(*list));
1699 list_add_tail(list, &d->entry);
1703 static declarator_t *make_declarator(var_t *var)
1705 declarator_t *d = xmalloc(sizeof(*d));
1708 d->func_type = NULL;
1713 static func_list_t *append_func(func_list_t *list, func_t *func)
1715 if (!func) return list;
1718 list = xmalloc( sizeof(*list) );
1721 list_add_tail( list, &func->entry );
1725 static func_t *make_func(var_t *def)
1727 func_t *f = xmalloc(sizeof(func_t));
1729 f->args = def->type->fields_or_args;
1730 f->ignore = parse_only;
1735 static type_t *make_class(char *name)
1737 type_t *c = make_type(RPC_FC_COCLASS, NULL);
1739 c->kind = TKIND_COCLASS;
1743 static type_t *make_safearray(type_t *type)
1745 type_t *sa = find_type_or_error("SAFEARRAY", 0);
1747 return make_type(pointer_default, sa);
1750 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1752 typelib_t *typelib = xmalloc(sizeof(*typelib));
1753 typelib->name = xstrdup(name);
1754 typelib->filename = NULL;
1755 typelib->attrs = attrs;
1756 list_init( &typelib->entries );
1757 list_init( &typelib->importlibs );
1763 static int hash_ident(const char *name)
1765 const char *p = name;
1767 /* a simple sum hash is probably good enough */
1772 return sum & (HASHMAX-1);
1775 /***** type repository *****/
1784 struct rtype *type_hash[HASHMAX];
1786 static type_t *reg_type(type_t *type, const char *name, int t)
1791 error_loc("registering named type without name\n");
1794 hash = hash_ident(name);
1795 nt = xmalloc(sizeof(struct rtype));
1799 nt->next = type_hash[hash];
1800 type_hash[hash] = nt;
1804 static int is_incomplete(const type_t *t)
1806 return !t->defined && (is_struct(t->type) || is_union(t->type));
1809 static void add_incomplete(type_t *t)
1811 struct typenode *tn = xmalloc(sizeof *tn);
1813 list_add_tail(&incomplete_types, &tn->entry);
1816 static void fix_type(type_t *t)
1818 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1819 type_t *ot = t->orig;
1821 t->fields_or_args = ot->fields_or_args;
1822 t->defined = ot->defined;
1826 static void fix_incomplete(void)
1828 struct typenode *tn, *next;
1830 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1836 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1838 const declarator_t *decl;
1839 int is_str = is_attr(attrs, ATTR_STRING);
1840 type_t *type = decl_spec->type;
1844 type_t *t = decl_spec->type;
1851 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1853 decl = LIST_ENTRY( list_head( decls ), const declarator_t, entry );
1854 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1859 /* We must generate names for tagless enum, struct or union.
1860 Typedef-ing a tagless enum, struct or union means we want the typedef
1861 to be included in a library hence the public attribute. */
1862 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1863 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1865 if (! is_attr(attrs, ATTR_PUBLIC))
1866 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1867 type->name = gen_name();
1869 else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1870 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1872 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1874 var_t *name = decl->var;
1879 cur = find_type(name->name, 0);
1881 error_loc("%s: redefinition error; original definition was at %s:%d\n",
1882 cur->name, cur->loc_info.input_name,
1883 cur->loc_info.line_number);
1885 /* set the attributes to allow set_type to do some checks on them */
1886 name->attrs = attrs;
1887 set_type(name, decl_spec, decl, 0);
1888 cur = alias(name->type, name->name);
1891 if (is_incomplete(cur))
1892 add_incomplete(cur);
1893 reg_type(cur, cur->name, 0);
1899 type_t *find_type(const char *name, int t)
1901 struct rtype *cur = type_hash[hash_ident(name)];
1902 while (cur && (cur->t != t || strcmp(cur->name, name)))
1904 return cur ? cur->type : NULL;
1907 static type_t *find_type_or_error(const char *name, int t)
1909 type_t *type = find_type(name, t);
1911 error_loc("type '%s' not found\n", name);
1917 static type_t *find_type_or_error2(char *name, int t)
1919 type_t *tp = find_type_or_error(name, t);
1924 int is_type(const char *name)
1926 return find_type(name, 0) != NULL;
1929 static type_t *get_type(unsigned char type, char *name, int t)
1933 tp = find_type(name, t);
1939 tp = make_type(type, NULL);
1941 if (!name) return tp;
1942 return reg_type(tp, name, t);
1945 static type_t *get_typev(unsigned char type, var_t *name, int t)
1952 return get_type(type, sname, t);
1955 /***** constant repository *****/
1960 struct rconst *next;
1963 struct rconst *const_hash[HASHMAX];
1965 static var_t *reg_const(var_t *var)
1970 error_loc("registering constant without name\n");
1973 hash = hash_ident(var->name);
1974 nc = xmalloc(sizeof(struct rconst));
1975 nc->name = var->name;
1977 nc->next = const_hash[hash];
1978 const_hash[hash] = nc;
1982 var_t *find_const(const char *name, int f)
1984 struct rconst *cur = const_hash[hash_ident(name)];
1985 while (cur && strcmp(cur->name, name))
1988 if (f) error_loc("constant '%s' not found\n", name);
1994 static void write_libid(const typelib_t *typelib)
1996 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
1997 write_guid(idfile, "LIBID", typelib->name, uuid);
2000 static void write_clsid(type_t *cls)
2002 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2003 write_guid(idfile, "CLSID", cls->name, uuid);
2006 static void write_diid(type_t *iface)
2008 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2009 write_guid(idfile, "DIID", iface->name, uuid);
2012 static void write_iid(type_t *iface)
2014 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2015 write_guid(idfile, "IID", iface->name, uuid);
2018 static char *gen_name(void)
2020 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2021 static unsigned long n = 0;
2022 static const char *file_id;
2028 char *dst = dup_basename(input_name, ".idl");
2032 if (! isalnum((unsigned char) *dst))
2035 size = sizeof format - 7 + strlen(file_id) + 8;
2038 name = xmalloc(size);
2039 sprintf(name, format, file_id, n++);
2045 unsigned int dce_compatible : 1;
2046 unsigned int acf : 1;
2047 unsigned int on_interface : 1;
2048 unsigned int on_function : 1;
2049 unsigned int on_arg : 1;
2050 unsigned int on_type : 1;
2051 unsigned int on_enum : 1;
2052 unsigned int on_struct : 1;
2053 unsigned int on_union : 1;
2054 unsigned int on_field : 1;
2055 unsigned int on_library : 1;
2056 unsigned int on_dispinterface : 1;
2057 unsigned int on_module : 1;
2058 unsigned int on_coclass : 1;
2059 const char *display_name;
2062 struct allowed_attr allowed_attr[] =
2064 /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
2065 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2066 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2067 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
2068 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2069 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2070 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2071 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2072 /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2073 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2074 /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
2075 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
2076 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2077 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
2078 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2079 /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2080 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2081 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2082 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2083 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2084 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2085 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2086 /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2087 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2088 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2089 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2090 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2091 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2092 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2093 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2094 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2095 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
2096 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2097 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2098 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2099 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2100 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2101 /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2102 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2103 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2104 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2105 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2106 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2107 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2108 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2109 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2110 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2111 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2112 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2113 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2114 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2115 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2116 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2117 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2118 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2119 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2120 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2121 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2122 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2123 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2124 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2125 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2126 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2127 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2128 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2129 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2130 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2131 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2132 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
2133 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2134 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2135 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2136 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2139 const char *get_attr_display_name(enum attr_type type)
2141 return allowed_attr[type].display_name;
2144 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2147 if (!attrs) return attrs;
2148 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2150 if (!allowed_attr[attr->type].on_interface)
2151 error_loc("inapplicable attribute %s for interface %s\n",
2152 allowed_attr[attr->type].display_name, name);
2157 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2160 if (!attrs) return attrs;
2161 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2163 if (!allowed_attr[attr->type].on_function)
2164 error_loc("inapplicable attribute %s for function %s\n",
2165 allowed_attr[attr->type].display_name, name);
2170 static void check_arg(var_t *arg)
2172 const type_t *t = arg->type;
2175 if (t->type == 0 && ! is_var_ptr(arg))
2176 error_loc("argument '%s' has void type\n", arg->name);
2180 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2182 if (!allowed_attr[attr->type].on_arg)
2183 error_loc("inapplicable attribute %s for argument %s\n",
2184 allowed_attr[attr->type].display_name, arg->name);
2189 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2192 if (!attrs) return attrs;
2193 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2195 if (!allowed_attr[attr->type].on_type)
2196 error_loc("inapplicable attribute %s for typedef\n",
2197 allowed_attr[attr->type].display_name);
2202 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2205 if (!attrs) return attrs;
2206 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2208 if (!allowed_attr[attr->type].on_enum)
2209 error_loc("inapplicable attribute %s for enum\n",
2210 allowed_attr[attr->type].display_name);
2215 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2218 if (!attrs) return attrs;
2219 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2221 if (!allowed_attr[attr->type].on_struct)
2222 error_loc("inapplicable attribute %s for struct\n",
2223 allowed_attr[attr->type].display_name);
2228 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2231 if (!attrs) return attrs;
2232 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2234 if (!allowed_attr[attr->type].on_union)
2235 error_loc("inapplicable attribute %s for union\n",
2236 allowed_attr[attr->type].display_name);
2241 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2244 if (!attrs) return attrs;
2245 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2247 if (!allowed_attr[attr->type].on_field)
2248 error_loc("inapplicable attribute %s for field %s\n",
2249 allowed_attr[attr->type].display_name, name);
2254 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2257 if (!attrs) return attrs;
2258 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2260 if (!allowed_attr[attr->type].on_library)
2261 error_loc("inapplicable attribute %s for library %s\n",
2262 allowed_attr[attr->type].display_name, name);
2267 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2270 if (!attrs) return attrs;
2271 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2273 if (!allowed_attr[attr->type].on_dispinterface)
2274 error_loc("inapplicable attribute %s for dispinterface %s\n",
2275 allowed_attr[attr->type].display_name, name);
2280 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2283 if (!attrs) return attrs;
2284 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2286 if (!allowed_attr[attr->type].on_module)
2287 error_loc("inapplicable attribute %s for module %s\n",
2288 allowed_attr[attr->type].display_name, name);
2293 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2296 if (!attrs) return attrs;
2297 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2299 if (!allowed_attr[attr->type].on_coclass)
2300 error_loc("inapplicable attribute %s for coclass %s\n",
2301 allowed_attr[attr->type].display_name, name);
2306 static int is_allowed_conf_type(const type_t *type)
2327 static int is_ptr_guid_type(const type_t *type)
2329 unsigned int align = 0;
2332 if (type->kind == TKIND_ALIAS)
2334 else if (is_ptr(type))
2342 return (type_memsize(type, &align) == 16);
2345 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2348 struct expr_loc expr_loc;
2350 expr_loc.attr = attr_name;
2351 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2353 if (dim->type != EXPR_VOID)
2355 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2356 if (!is_allowed_conf_type(expr_type))
2357 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2363 static void check_remoting_fields(const var_t *var, type_t *type);
2365 /* checks that properties common to fields and arguments are consistent */
2366 static void check_field_common(const type_t *container_type,
2367 const char *container_name, const var_t *arg)
2369 type_t *type = arg->type;
2370 int is_wire_marshal = 0;
2371 int is_context_handle = 0;
2372 const char *container_type_name = NULL;
2374 if (is_struct(container_type->type))
2375 container_type_name = "struct";
2376 else if (is_union(container_type->type))
2377 container_type_name = "union";
2378 else if (container_type->type == RPC_FC_FUNCTION)
2379 container_type_name = "function";
2381 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2382 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2383 error_loc_info(&arg->loc_info,
2384 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2387 if (is_attr(arg->attrs, ATTR_SIZEIS))
2389 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2390 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2392 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2394 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2395 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2397 if (is_attr(arg->attrs, ATTR_IIDIS))
2399 struct expr_loc expr_loc;
2400 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2401 if (expr->type != EXPR_VOID)
2403 const type_t *expr_type;
2405 expr_loc.attr = "iid_is";
2406 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2407 if (!expr_type || !is_ptr_guid_type(expr_type))
2408 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2411 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2413 struct expr_loc expr_loc;
2414 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2415 if (expr->type != EXPR_VOID)
2417 const type_t *expr_type;
2419 expr_loc.attr = "switch_is";
2420 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2421 if (!expr_type || !is_allowed_conf_type(expr_type))
2422 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2427 /* get fundamental type for the argument */
2430 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2432 is_wire_marshal = 1;
2435 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2437 is_context_handle = 1;
2440 if (type->kind == TKIND_ALIAS)
2442 else if (is_ptr(type) || is_array(type))
2448 if (type->type == 0 && !is_attr(arg->attrs, ATTR_IIDIS) && !is_wire_marshal && !is_context_handle)
2449 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot derive from void *\n", arg->name, container_type_name, container_name);
2450 else if (type->type == RPC_FC_FUNCTION)
2451 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot be a function pointer\n", arg->name, container_type_name, container_name);
2452 else if (!is_wire_marshal && (is_struct(type->type) || is_union(type->type)))
2453 check_remoting_fields(arg, type);
2456 static void check_remoting_fields(const var_t *var, type_t *type)
2459 const var_list_t *fields = NULL;
2461 /* find the real type */
2462 while (type->kind == TKIND_ALIAS)
2468 type->checked = TRUE;
2470 if (is_struct(type->type))
2472 if (type_is_defined(type))
2473 fields = type_struct_get_fields(type);
2475 error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2477 else if (is_union(type->type))
2478 fields = type_union_get_cases(type);
2480 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2481 if (field->type) check_field_common(type, type->name, field);
2484 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2485 static void check_remoting_args(const func_t *func)
2487 const char *funcname = func->def->name;
2490 if (func->args) LIST_FOR_EACH_ENTRY( arg, func->args, const var_t, entry )
2493 const type_t *type = arg->type;
2495 /* get pointer level and fundamental type for the argument */
2498 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2500 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2502 if (type->kind == TKIND_ALIAS)
2504 else if (is_ptr(type))
2513 /* check that [out] parameters have enough pointer levels */
2514 if (is_attr(arg->attrs, ATTR_OUT))
2516 if (!is_array(type))
2519 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2520 if (type->type == RPC_FC_IP && ptr_level == 1)
2521 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2525 check_field_common(func->def->type, funcname, arg);
2529 static void add_explicit_handle_if_necessary(func_t *func)
2531 const var_t* explicit_handle_var;
2532 const var_t* explicit_generic_handle_var = NULL;
2533 const var_t* context_handle_var = NULL;
2535 /* check for a defined binding handle */
2536 explicit_handle_var = get_explicit_handle_var(func);
2537 if (!explicit_handle_var)
2539 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2540 if (!explicit_generic_handle_var)
2542 context_handle_var = get_context_handle_var(func);
2543 if (!context_handle_var)
2545 /* no explicit handle specified so add
2546 * "[in] handle_t IDL_handle" as the first parameter to the
2548 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2549 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2550 idl_handle->type = find_type_or_error("handle_t", 0);
2551 function_add_head_arg(func, idl_handle);
2557 void check_functions(const type_t *iface)
2559 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE) && iface->funcs)
2562 LIST_FOR_EACH_ENTRY( func, iface->funcs, func_t, entry )
2563 add_explicit_handle_if_necessary(func);
2565 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2568 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2570 if (!is_attr(func->def->attrs, ATTR_LOCAL))
2571 check_remoting_args(func);
2576 static void check_all_user_types(const statement_list_t *stmts)
2578 const statement_t *stmt;
2580 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2582 if (stmt->type == STMT_LIBRARY)
2583 check_all_user_types(stmt->u.lib->stmts);
2584 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
2587 const func_list_t *fs = stmt->u.type->funcs;
2588 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2589 check_for_additional_prototype_types(f->args);
2594 int is_valid_uuid(const char *s)
2598 for (i = 0; i < 36; ++i)
2599 if (i == 8 || i == 13 || i == 18 || i == 23)
2605 if (!isxdigit(s[i]))
2608 return s[i] == '\0';
2611 static statement_t *make_statement(enum statement_type type)
2613 statement_t *stmt = xmalloc(sizeof(*stmt));
2618 static statement_t *make_statement_type_decl(type_t *type)
2620 statement_t *stmt = make_statement(STMT_TYPE);
2621 stmt->u.type = type;
2625 static statement_t *make_statement_reference(type_t *type)
2627 statement_t *stmt = make_statement(STMT_TYPEREF);
2628 stmt->u.type = type;
2632 static statement_t *make_statement_declaration(var_t *var)
2634 statement_t *stmt = make_statement(STMT_DECLARATION);
2636 if (var->stgclass == STG_EXTERN && var->eval)
2637 warning("'%s' initialised and declared extern\n", var->name);
2638 if (is_const_decl(var))
2643 else if ((var->stgclass == STG_NONE || var->stgclass == STG_REGISTER) &&
2644 var->type->type != RPC_FC_FUNCTION)
2645 error_loc("instantiation of data is illegal\n");
2649 static statement_t *make_statement_library(typelib_t *typelib)
2651 statement_t *stmt = make_statement(STMT_LIBRARY);
2652 stmt->u.lib = typelib;
2656 static statement_t *make_statement_cppquote(const char *str)
2658 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2663 static statement_t *make_statement_importlib(const char *str)
2665 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2670 static statement_t *make_statement_import(const char *str)
2672 statement_t *stmt = make_statement(STMT_IMPORT);
2677 static statement_t *make_statement_module(type_t *type)
2679 statement_t *stmt = make_statement(STMT_MODULE);
2680 stmt->u.type = type;
2684 static statement_t *process_typedefs(declarator_list_t *decls)
2686 declarator_t *decl, *next;
2688 type_list_t **type_list;
2690 if (!decls) return NULL;
2692 stmt = make_statement(STMT_TYPEDEF);
2693 stmt->u.type_list = NULL;
2694 type_list = &stmt->u.type_list;
2696 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2698 var_t *var = decl->var;
2699 type_t *type = find_type_or_error(var->name, 0);
2700 *type_list = xmalloc(sizeof(type_list_t));
2701 (*type_list)->type = type;
2702 (*type_list)->next = NULL;
2704 if (! parse_only && do_header)
2705 write_typedef(type);
2706 if (in_typelib && is_attr(type->attrs, ATTR_PUBLIC))
2707 add_typelib_entry(type);
2709 type_list = &(*type_list)->next;
2717 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2719 if (!stmt) return list;
2722 list = xmalloc( sizeof(*list) );
2725 list_add_tail( list, &stmt->entry );
2729 static func_list_t *append_func_from_statement(func_list_t *list, statement_t *stmt)
2731 if (stmt->type == STMT_DECLARATION)
2733 var_t *var = stmt->u.var;
2734 if (var->stgclass == STG_NONE && var->type->type == RPC_FC_FUNCTION)
2736 check_function_attrs(var->name, var->type->attrs);
2737 return append_func(list, make_func(stmt->u.var));
2743 void init_loc_info(loc_info_t *i)
2745 i->input_name = input_name ? input_name : "stdin";
2746 i->line_number = line_number;
2747 i->near_text = parser_text;
2750 static void check_def(const type_t *t)
2753 error_loc("%s: redefinition error; original definition was at %s:%d\n",
2754 t->name, t->loc_info.input_name, t->loc_info.line_number);