push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / tools / widl / parser.y
blob45471bb7ff465a07850a7cbef86e05911bf9745e
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <string.h>
30 #ifdef HAVE_ALLOCA_H
31 #include <alloca.h>
32 #endif
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "typelib.h"
39 #include "typegen.h"
41 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
63 # endif
64 #endif
66 #define YYERROR_VERBOSE
68 unsigned char pointer_default = RPC_FC_UP;
69 static int is_object_interface = FALSE;
70 /* are we inside a library block? */
71 static int is_inside_library = FALSE;
73 typedef struct list typelist_t;
74 struct typenode {
75 type_t *type;
76 struct list entry;
79 typelist_t incomplete_types = LIST_INIT(incomplete_types);
81 static void add_incomplete(type_t *t);
82 static void fix_incomplete(void);
84 static str_list_t *append_str(str_list_t *list, char *str);
85 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
86 static attr_t *make_attr(enum attr_type type);
87 static attr_t *make_attrv(enum attr_type type, unsigned long val);
88 static attr_t *make_attrp(enum attr_type type, void *val);
89 static expr_t *make_expr(enum expr_type type);
90 static expr_t *make_exprl(enum expr_type type, long val);
91 static expr_t *make_exprd(enum expr_type type, double val);
92 static expr_t *make_exprs(enum expr_type type, char *val);
93 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
94 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
95 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
96 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
97 static type_t *make_type(unsigned char type, type_t *ref);
98 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
99 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
100 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr, int top);
101 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
102 static ifref_t *make_ifref(type_t *iface);
103 static var_list_t *append_var(var_list_t *list, var_t *var);
104 static var_t *make_var(char *name);
105 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
106 static pident_t *make_pident(var_t *var);
107 static func_list_t *append_func(func_list_t *list, func_t *func);
108 static func_t *make_func(var_t *def, var_list_t *args);
109 static type_t *make_class(char *name);
110 static type_t *make_safearray(type_t *type);
111 static type_t *make_builtin(char *name);
112 static type_t *make_int(int sign);
114 static type_t *reg_type(type_t *type, const char *name, int t);
115 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
116 static type_t *find_type(const char *name, int t);
117 static type_t *find_type2(char *name, int t);
118 static type_t *get_type(unsigned char type, char *name, int t);
119 static type_t *get_typev(unsigned char type, var_t *name, int t);
120 static int get_struct_type(var_list_t *fields);
122 static var_t *reg_const(var_t *var);
123 static var_t *find_const(char *name, int f);
125 static void write_libid(const char *name, const attr_list_t *attr);
126 static void write_clsid(type_t *cls);
127 static void write_diid(type_t *iface);
128 static void write_iid(type_t *iface);
130 static int compute_method_indexes(type_t *iface);
131 static char *gen_name(void);
132 static void process_typedefs(var_list_t *names);
133 static void check_arg(var_t *arg);
134 static void check_functions(const type_t *iface);
135 static void check_all_user_types(ifref_list_t *ifaces);
136 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs);
137 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
138 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
139 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
140 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs);
141 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
142 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs);
143 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs);
144 const char *get_attr_display_name(enum attr_type type);
146 #define tsENUM 1
147 #define tsSTRUCT 2
148 #define tsUNION 3
151 %union {
152 attr_t *attr;
153 attr_list_t *attr_list;
154 str_list_t *str_list;
155 expr_t *expr;
156 expr_list_t *expr_list;
157 array_dims_t *array_dims;
158 type_t *type;
159 var_t *var;
160 var_list_t *var_list;
161 pident_t *pident;
162 pident_list_t *pident_list;
163 func_t *func;
164 func_list_t *func_list;
165 ifref_t *ifref;
166 ifref_list_t *ifref_list;
167 char *str;
168 UUID *uuid;
169 unsigned int num;
170 double dbl;
171 interface_info_t ifinfo;
174 %token <str> aIDENTIFIER
175 %token <str> aKNOWNTYPE
176 %token <num> aNUM aHEXNUM
177 %token <dbl> aDOUBLE
178 %token <str> aSTRING
179 %token <uuid> aUUID
180 %token aEOF
181 %token SHL SHR
182 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
183 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
184 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
185 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
186 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
187 %token tDEFAULT
188 %token tDEFAULTCOLLELEM
189 %token tDEFAULTVALUE
190 %token tDEFAULTVTABLE
191 %token tDISPLAYBIND
192 %token tDISPINTERFACE
193 %token tDLLNAME tDOUBLE tDUAL
194 %token tENDPOINT
195 %token tENTRY tENUM tERRORSTATUST
196 %token tEXPLICITHANDLE tEXTERN
197 %token tFALSE
198 %token tFASTCALL
199 %token tFLOAT
200 %token tHANDLE
201 %token tHANDLET
202 %token tHELPCONTEXT tHELPFILE
203 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
204 %token tHIDDEN
205 %token tHYPER tID tIDEMPOTENT
206 %token tIIDIS
207 %token tIMMEDIATEBIND
208 %token tIMPLICITHANDLE
209 %token tIMPORT tIMPORTLIB
210 %token tIN tINLINE
211 %token tINPUTSYNC
212 %token tINT tINT64
213 %token tINTERFACE
214 %token tLCID
215 %token tLENGTHIS tLIBRARY
216 %token tLOCAL
217 %token tLONG
218 %token tMETHODS
219 %token tMODULE
220 %token tNONBROWSABLE
221 %token tNONCREATABLE
222 %token tNONEXTENSIBLE
223 %token tOBJECT tODL tOLEAUTOMATION
224 %token tOPTIONAL
225 %token tOUT
226 %token tPASCAL
227 %token tPOINTERDEFAULT
228 %token tPROPERTIES
229 %token tPROPGET tPROPPUT tPROPPUTREF
230 %token tPTR
231 %token tPUBLIC
232 %token tRANGE
233 %token tREADONLY tREF
234 %token tREQUESTEDIT
235 %token tRESTRICTED
236 %token tRETVAL
237 %token tSAFEARRAY
238 %token tSHORT
239 %token tSIGNED
240 %token tSINGLE
241 %token tSIZEIS tSIZEOF
242 %token tSMALL
243 %token tSOURCE
244 %token tSTDCALL
245 %token tSTRICTCONTEXTHANDLE
246 %token tSTRING tSTRUCT
247 %token tSWITCH tSWITCHIS tSWITCHTYPE
248 %token tTRANSMITAS
249 %token tTRUE
250 %token tTYPEDEF
251 %token tUNION
252 %token tUNIQUE
253 %token tUNSIGNED
254 %token tUUID
255 %token tV1ENUM
256 %token tVARARG
257 %token tVERSION
258 %token tVOID
259 %token tWCHAR tWIREMARSHAL
261 %type <attr> attribute
262 %type <attr_list> m_attributes attributes attrib_list
263 %type <str_list> str_list
264 %type <expr> m_expr expr expr_const
265 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
266 %type <array_dims> array array_list
267 %type <ifinfo> interfacehdr
268 %type <type> inherit interface interfacedef interfacedec
269 %type <type> dispinterface dispinterfacehdr dispinterfacedef
270 %type <type> module modulehdr moduledef
271 %type <type> base_type int_std
272 %type <type> enumdef structdef uniondef
273 %type <type> type
274 %type <ifref> coclass_int
275 %type <ifref_list> gbl_statements coclass_ints
276 %type <var> arg field s_field case enum constdef externdef
277 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
278 %type <var> m_ident t_ident ident
279 %type <pident> pident func_ident direct_ident
280 %type <pident_list> pident_list
281 %type <func> funcdef
282 %type <func_list> int_statements dispint_meths
283 %type <type> coclass coclasshdr coclassdef
284 %type <num> pointer_type version
285 %type <str> libraryhdr callconv
286 %type <uuid> uuid_string
287 %type <num> import_start
289 %left ','
290 %right '?' ':'
291 %left '|'
292 %left '&'
293 %left '-' '+'
294 %left '*' '/'
295 %left SHL SHR
296 %right '~'
297 %right CAST
298 %right PPTR
299 %right NEG
300 %right ADDRESSOF
304 input: gbl_statements { fix_incomplete();
305 check_all_user_types($1);
306 write_proxies($1);
307 write_client($1);
308 write_server($1);
309 write_dlldata($1);
313 gbl_statements: { $$ = NULL; }
314 | gbl_statements interfacedec { $$ = $1; }
315 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
316 | gbl_statements coclass ';' { $$ = $1;
317 reg_type($2, $2->name, 0);
318 if (!parse_only && do_header) write_coclass_forward($2);
320 | gbl_statements coclassdef { $$ = $1;
321 add_typelib_entry($2);
322 reg_type($2, $2->name, 0);
323 if (!parse_only && do_header) write_coclass_forward($2);
325 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
326 | gbl_statements librarydef { $$ = $1; }
327 | gbl_statements statement { $$ = $1; }
330 imp_statements: {}
331 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
332 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
333 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
334 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
335 reg_type($2, $2->name, 0);
336 if (!parse_only && do_header) write_coclass_forward($2);
338 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
339 | imp_statements statement {}
340 | imp_statements importlib {}
341 | imp_statements librarydef {}
344 int_statements: { $$ = NULL; }
345 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
346 | int_statements statement { $$ = $1; }
349 semicolon_opt:
350 | ';'
353 statement: constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
354 | cppquote {}
355 | enumdef ';' { if (!parse_only && do_header) {
356 write_type_def_or_decl(header, $1, FALSE, NULL);
357 fprintf(header, ";\n\n");
360 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
361 | import {}
362 | structdef ';' { if (!parse_only && do_header) {
363 write_type_def_or_decl(header, $1, FALSE, NULL);
364 fprintf(header, ";\n\n");
367 | typedef ';' {}
368 | uniondef ';' { if (!parse_only && do_header) {
369 write_type_def_or_decl(header, $1, FALSE, NULL);
370 fprintf(header, ";\n\n");
375 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
377 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
378 $$ = do_import($2);
379 if (!$$) yychar = aEOF;
383 import: import_start imp_statements aEOF
384 { if ($1) pop_import(); }
387 importlib: tIMPORTLIB '(' aSTRING ')'
388 semicolon_opt { if(!parse_only) add_importlib($3); }
391 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
393 library_start: attributes libraryhdr '{' { check_library_attrs($2, $1);
394 if (!parse_only) start_typelib($2, $1);
395 if (!parse_only && do_header) write_library($2, $1);
396 if (!parse_only && do_idfile) write_libid($2, $1);
397 is_inside_library = TRUE;
400 librarydef: library_start imp_statements '}'
401 semicolon_opt { if (!parse_only) end_typelib(); is_inside_library = FALSE; }
404 m_args: { $$ = NULL; }
405 | args
408 no_args: tVOID { $$ = NULL; }
411 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
412 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
413 | no_args
416 /* split into two rules to get bison to resolve a tVOID conflict */
417 arg: attributes type pident array { $$ = $3->var;
418 $$->attrs = $1;
419 set_type($$, $2, $3, $4, TRUE);
420 free($3);
422 | type pident array { $$ = $2->var;
423 set_type($$, $1, $2, $3, TRUE);
424 free($2);
428 array: { $$ = NULL; }
429 | '[' array_list ']' { $$ = $2; }
430 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
433 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
434 | array_list ',' expr { $$ = append_array( $1, $3 ); }
435 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
438 m_attributes: { $$ = NULL; }
439 | attributes
442 attributes:
443 '[' attrib_list ']' { $$ = $2;
444 if (!$$)
445 error_loc("empty attribute lists unsupported\n");
449 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
450 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
451 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
454 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
455 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
458 attribute: { $$ = NULL; }
459 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
460 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
461 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
462 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
463 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
464 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
465 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
466 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
467 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
468 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
469 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
470 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
471 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
472 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
473 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
474 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
475 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
476 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
477 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
478 | tDUAL { $$ = make_attr(ATTR_DUAL); }
479 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
480 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
481 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
482 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
483 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
484 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
485 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
486 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
487 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
488 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
489 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
490 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
491 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
492 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
493 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
494 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
495 | tIN { $$ = make_attr(ATTR_IN); }
496 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
497 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
498 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
499 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
500 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
501 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
502 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
503 | tODL { $$ = make_attr(ATTR_ODL); }
504 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
505 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
506 | tOUT { $$ = make_attr(ATTR_OUT); }
507 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
508 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
509 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
510 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
511 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
512 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
513 list = append_expr( list, $5 );
514 $$ = make_attrp(ATTR_RANGE, list); }
515 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
516 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
517 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
518 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
519 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
520 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
521 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
522 | tSTRING { $$ = make_attr(ATTR_STRING); }
523 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
524 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
525 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
526 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
527 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
528 | tVARARG { $$ = make_attr(ATTR_VARARG); }
529 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
530 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
531 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
534 uuid_string:
535 aUUID
536 | aSTRING { if (!is_valid_uuid($1))
537 error_loc("invalid UUID: %s\n", $1);
538 $$ = parse_uuid($1); }
541 callconv: tCDECL { $$ = $<str>1; }
542 | tFASTCALL { $$ = $<str>1; }
543 | tPASCAL { $$ = $<str>1; }
544 | tSTDCALL { $$ = $<str>1; }
547 cases: { $$ = NULL; }
548 | cases case { $$ = append_var( $1, $2 ); }
551 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
552 $$ = $4; if (!$$) $$ = make_var(NULL);
553 $$->attrs = append_attr( $$->attrs, a );
555 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
556 $$ = $3; if (!$$) $$ = make_var(NULL);
557 $$->attrs = append_attr( $$->attrs, a );
561 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
562 set_type($$, $2, NULL, NULL, FALSE);
563 $$->eval = $5;
567 enums: { $$ = NULL; }
568 | enum_list ',' { $$ = $1; }
569 | enum_list
572 enum_list: enum { if (!$1->eval)
573 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
574 $$ = append_var( NULL, $1 );
576 | enum_list ',' enum { if (!$3->eval)
578 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
579 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
581 $$ = append_var( $1, $3 );
585 enum: ident '=' expr_const { $$ = reg_const($1);
586 $$->eval = $3;
587 $$->type = make_int(0);
589 | ident { $$ = reg_const($1);
590 $$->type = make_int(0);
594 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
595 $$->kind = TKIND_ENUM;
596 $$->fields_or_args = $4;
597 $$->defined = TRUE;
598 if(in_typelib)
599 add_typelib_entry($$);
603 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
604 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
608 exprs: { $$ = make_expr(EXPR_VOID); }
609 | expr_list
612 expr_list: expr
613 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
617 m_expr: { $$ = make_expr(EXPR_VOID); }
618 | expr
621 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
622 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
623 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
624 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
625 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
626 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
627 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
628 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
629 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
630 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
631 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
632 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
633 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
634 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
635 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
636 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
637 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
638 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
639 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
640 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
641 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
642 | '(' expr ')' { $$ = $2; }
645 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
646 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
649 expr_const: expr { $$ = $1;
650 if (!$$->is_const)
651 error_loc("expression is not constant\n");
655 externdef: tEXTERN tCONST type ident { $$ = $4;
656 set_type($$, $3, NULL, NULL, FALSE);
660 fields: { $$ = NULL; }
661 | fields field { $$ = append_var( $1, $2 ); }
664 field: s_field ';' { $$ = $1; }
665 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
666 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
667 | ';' { $$ = NULL; }
670 s_field: m_attributes type pident array { $$ = $3->var;
671 $$->attrs = check_field_attrs($$->name, $1);
672 set_type($$, $2, $3, $4, FALSE);
673 free($3);
677 funcdef:
678 m_attributes type pident { var_t *v = $3->var;
679 var_list_t *args = $3->args;
680 v->attrs = check_function_attrs(v->name, $1);
681 set_type(v, $2, $3, NULL, FALSE);
682 free($3);
683 $$ = make_func(v, args);
687 m_ident: { $$ = NULL; }
688 | ident
691 t_ident: { $$ = NULL; }
692 | aIDENTIFIER { $$ = make_var($1); }
693 | aKNOWNTYPE { $$ = make_var($1); }
696 ident: aIDENTIFIER { $$ = make_var($1); }
697 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
698 | aKNOWNTYPE { $$ = make_var($<str>1); }
701 base_type: tBYTE { $$ = make_builtin($<str>1); }
702 | tWCHAR { $$ = make_builtin($<str>1); }
703 | int_std
704 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
705 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
706 switch ($$->type) {
707 case RPC_FC_CHAR: break;
708 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
709 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
710 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
711 case RPC_FC_HYPER:
712 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
714 $$ = alias($$, "MIDL_uhyper");
715 $$->sign = 0;
717 break;
718 default: break;
721 | tUNSIGNED { $$ = make_int(-1); }
722 | tFLOAT { $$ = make_builtin($<str>1); }
723 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
724 | tDOUBLE { $$ = make_builtin($<str>1); }
725 | tBOOLEAN { $$ = make_builtin($<str>1); }
726 | tERRORSTATUST { $$ = make_builtin($<str>1); }
727 | tHANDLET { $$ = make_builtin($<str>1); }
730 m_int:
731 | tINT
734 int_std: tINT { $$ = make_builtin($<str>1); }
735 | tSHORT m_int { $$ = make_builtin($<str>1); }
736 | tSMALL { $$ = make_builtin($<str>1); }
737 | tLONG m_int { $$ = make_builtin($<str>1); }
738 | tHYPER m_int { $$ = make_builtin($<str>1); }
739 | tINT64 { $$ = make_builtin($<str>1); }
740 | tCHAR { $$ = make_builtin($<str>1); }
743 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
744 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
745 if ($$->defined) error_loc("multiple definition error\n");
746 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
750 coclasshdr: attributes coclass { $$ = $2;
751 $$->attrs = check_coclass_attrs($2->name, $1);
752 if (!parse_only && do_header)
753 write_coclass($$);
754 if (!parse_only && do_idfile)
755 write_clsid($$);
759 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
760 { $$ = $1;
761 $$->ifaces = $3;
762 $$->defined = TRUE;
766 coclass_ints: { $$ = NULL; }
767 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
770 coclass_int:
771 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
774 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
775 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
778 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
779 is_object_interface = TRUE;
780 $$ = $2;
781 if ($$->defined) error_loc("multiple definition error\n");
782 attrs = make_attr(ATTR_DISPINTERFACE);
783 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
784 $$->ref = find_type("IDispatch", 0);
785 if (!$$->ref) error_loc("IDispatch is undefined\n");
786 $$->defined = TRUE;
787 if (!parse_only && do_header) write_forward($$);
791 dispint_props: tPROPERTIES ':' { $$ = NULL; }
792 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
795 dispint_meths: tMETHODS ':' { $$ = NULL; }
796 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
799 dispinterfacedef: dispinterfacehdr '{'
800 dispint_props
801 dispint_meths
802 '}' { $$ = $1;
803 $$->fields_or_args = $3;
804 $$->funcs = $4;
805 if (!parse_only && do_header) write_dispinterface($$);
806 if (!parse_only && do_idfile) write_diid($$);
808 | dispinterfacehdr
809 '{' interface ';' '}' { $$ = $1;
810 $$->fields_or_args = $3->fields_or_args;
811 $$->funcs = $3->funcs;
812 if (!parse_only && do_header) write_dispinterface($$);
813 if (!parse_only && do_idfile) write_diid($$);
817 inherit: { $$ = NULL; }
818 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
821 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
822 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
825 interfacehdr: attributes interface { $$.interface = $2;
826 $$.old_pointer_default = pointer_default;
827 if (is_attr($1, ATTR_POINTERDEFAULT))
828 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
829 is_object_interface = is_object($1);
830 if ($2->defined) error_loc("multiple definition error\n");
831 $2->attrs = check_iface_attrs($2->name, $1);
832 $2->defined = TRUE;
833 if (!parse_only && do_header) write_forward($2);
837 interfacedef: interfacehdr inherit
838 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
839 $$->ref = $2;
840 $$->funcs = $4;
841 check_functions($$);
842 compute_method_indexes($$);
843 if (!parse_only && do_header) write_interface($$);
844 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
845 if (!parse_only && do_idfile) write_iid($$);
846 pointer_default = $1.old_pointer_default;
848 /* MIDL is able to import the definition of a base class from inside the
849 * definition of a derived class, I'll try to support it with this rule */
850 | interfacehdr ':' aIDENTIFIER
851 '{' import int_statements '}'
852 semicolon_opt { $$ = $1.interface;
853 $$->ref = find_type2($3, 0);
854 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
855 $$->funcs = $6;
856 compute_method_indexes($$);
857 if (!parse_only && do_header) write_interface($$);
858 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
859 if (!parse_only && do_idfile) write_iid($$);
860 pointer_default = $1.old_pointer_default;
862 | dispinterfacedef semicolon_opt { $$ = $1; }
865 interfacedec:
866 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
867 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
870 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
871 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
874 modulehdr: attributes module { $$ = $2;
875 $$->attrs = check_module_attrs($2->name, $1);
879 moduledef: modulehdr '{' int_statements '}'
880 semicolon_opt { $$ = $1;
881 $$->funcs = $3;
882 /* FIXME: if (!parse_only && do_header) write_module($$); */
886 pident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
887 | tCONST pident { $$ = $2; /* FIXME */ }
888 | callconv pident { $$ = $2;
889 if ($$->callconv) parser_warning("multiple calling conventions %s, %s for function %s\n", $$->callconv, $1, $$->var->name);
890 $$->callconv = $1;
892 | direct_ident
895 func_ident: direct_ident '(' m_args ')'
896 { $$ = $1;
897 $1->args = $3;
898 $1->is_func = TRUE;
902 direct_ident: ident { $$ = make_pident($1); }
903 | '(' pident ')' { $$ = $2; }
904 | func_ident { $$ = $1;
905 $$->func_ptr_level = $$->ptr_level;
906 $$->ptr_level = 0;
910 pident_list:
911 pident { $$ = append_pident( NULL, $1 ); }
912 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
915 pointer_type:
916 tREF { $$ = RPC_FC_RP; }
917 | tUNIQUE { $$ = RPC_FC_UP; }
918 | tPTR { $$ = RPC_FC_FP; }
921 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
922 /* overwrite RPC_FC_STRUCT with a more exact type */
923 $$->type = get_struct_type( $4 );
924 $$->kind = TKIND_RECORD;
925 $$->fields_or_args = $4;
926 $$->defined = TRUE;
927 if(in_typelib)
928 add_typelib_entry($$);
932 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
933 | aKNOWNTYPE { $$ = find_type($1, 0); }
934 | base_type { $$ = $1; }
935 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
936 | enumdef { $$ = $1; }
937 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
938 | structdef { $$ = $1; }
939 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
940 | uniondef { $$ = $1; }
941 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
942 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
945 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, check_typedef_attrs($2));
946 process_typedefs($4);
950 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
951 $$->kind = TKIND_UNION;
952 $$->fields_or_args = $4;
953 $$->defined = TRUE;
955 | tUNION t_ident
956 tSWITCH '(' s_field ')'
957 m_ident '{' cases '}' { var_t *u = $7;
958 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
959 $$->kind = TKIND_UNION;
960 if (!u) u = make_var( xstrdup("tagged_union") );
961 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
962 u->type->kind = TKIND_UNION;
963 u->type->fields_or_args = $9;
964 u->type->defined = TRUE;
965 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
966 $$->fields_or_args = append_var( $$->fields_or_args, u );
967 $$->defined = TRUE;
971 version:
972 aNUM { $$ = MAKEVERSION($1, 0); }
973 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
978 static void decl_builtin(const char *name, unsigned char type)
980 type_t *t = make_type(type, NULL);
981 t->name = xstrdup(name);
982 reg_type(t, name, 0);
985 static type_t *make_builtin(char *name)
987 /* NAME is strdup'd in the lexer */
988 type_t *t = duptype(find_type(name, 0), 0);
989 t->name = name;
990 return t;
993 static type_t *make_int(int sign)
995 type_t *t = duptype(find_type("int", 0), 1);
997 t->sign = sign;
998 if (sign < 0)
999 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
1001 return t;
1004 void init_types(void)
1006 decl_builtin("void", 0);
1007 decl_builtin("byte", RPC_FC_BYTE);
1008 decl_builtin("wchar_t", RPC_FC_WCHAR);
1009 decl_builtin("int", RPC_FC_LONG); /* win32 */
1010 decl_builtin("short", RPC_FC_SHORT);
1011 decl_builtin("small", RPC_FC_SMALL);
1012 decl_builtin("long", RPC_FC_LONG);
1013 decl_builtin("hyper", RPC_FC_HYPER);
1014 decl_builtin("__int64", RPC_FC_HYPER);
1015 decl_builtin("char", RPC_FC_CHAR);
1016 decl_builtin("float", RPC_FC_FLOAT);
1017 decl_builtin("double", RPC_FC_DOUBLE);
1018 decl_builtin("boolean", RPC_FC_BYTE);
1019 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1020 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1023 static str_list_t *append_str(str_list_t *list, char *str)
1025 struct str_list_entry_t *entry;
1027 if (!str) return list;
1028 if (!list)
1030 list = xmalloc( sizeof(*list) );
1031 list_init( list );
1033 entry = xmalloc( sizeof(*entry) );
1034 entry->str = str;
1035 list_add_tail( list, &entry->entry );
1036 return list;
1039 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1041 attr_t *attr_existing;
1042 if (!attr) return list;
1043 if (!list)
1045 list = xmalloc( sizeof(*list) );
1046 list_init( list );
1048 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1049 if (attr_existing->type == attr->type)
1051 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1052 /* use the last attribute, like MIDL does */
1053 list_remove(&attr_existing->entry);
1054 break;
1056 list_add_tail( list, &attr->entry );
1057 return list;
1060 static attr_t *make_attr(enum attr_type type)
1062 attr_t *a = xmalloc(sizeof(attr_t));
1063 a->type = type;
1064 a->u.ival = 0;
1065 return a;
1068 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1070 attr_t *a = xmalloc(sizeof(attr_t));
1071 a->type = type;
1072 a->u.ival = val;
1073 return a;
1076 static attr_t *make_attrp(enum attr_type type, void *val)
1078 attr_t *a = xmalloc(sizeof(attr_t));
1079 a->type = type;
1080 a->u.pval = val;
1081 return a;
1084 static expr_t *make_expr(enum expr_type type)
1086 expr_t *e = xmalloc(sizeof(expr_t));
1087 e->type = type;
1088 e->ref = NULL;
1089 e->u.lval = 0;
1090 e->is_const = FALSE;
1091 e->cval = 0;
1092 return e;
1095 static expr_t *make_exprl(enum expr_type type, long val)
1097 expr_t *e = xmalloc(sizeof(expr_t));
1098 e->type = type;
1099 e->ref = NULL;
1100 e->u.lval = val;
1101 e->is_const = FALSE;
1102 /* check for numeric constant */
1103 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1104 /* make sure true/false value is valid */
1105 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1106 e->is_const = TRUE;
1107 e->cval = val;
1109 return e;
1112 static expr_t *make_exprd(enum expr_type type, double val)
1114 expr_t *e = xmalloc(sizeof(expr_t));
1115 e->type = type;
1116 e->ref = NULL;
1117 e->u.dval = val;
1118 e->is_const = TRUE;
1119 e->cval = val;
1120 return e;
1123 static expr_t *make_exprs(enum expr_type type, char *val)
1125 expr_t *e;
1126 e = xmalloc(sizeof(expr_t));
1127 e->type = type;
1128 e->ref = NULL;
1129 e->u.sval = val;
1130 e->is_const = FALSE;
1131 /* check for predefined constants */
1132 if (type == EXPR_IDENTIFIER) {
1133 var_t *c = find_const(val, 0);
1134 if (c) {
1135 e->u.sval = c->name;
1136 free(val);
1137 e->is_const = TRUE;
1138 e->cval = c->eval->cval;
1141 return e;
1144 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1146 expr_t *e;
1147 e = xmalloc(sizeof(expr_t));
1148 e->type = type;
1149 e->ref = expr;
1150 e->u.tref = tref;
1151 e->is_const = FALSE;
1152 /* check for cast of constant expression */
1153 if (type == EXPR_SIZEOF) {
1154 switch (tref->type) {
1155 case RPC_FC_BYTE:
1156 case RPC_FC_CHAR:
1157 case RPC_FC_SMALL:
1158 case RPC_FC_USMALL:
1159 e->is_const = TRUE;
1160 e->cval = 1;
1161 break;
1162 case RPC_FC_WCHAR:
1163 case RPC_FC_USHORT:
1164 case RPC_FC_SHORT:
1165 e->is_const = TRUE;
1166 e->cval = 2;
1167 break;
1168 case RPC_FC_LONG:
1169 case RPC_FC_ULONG:
1170 case RPC_FC_FLOAT:
1171 case RPC_FC_ERROR_STATUS_T:
1172 e->is_const = TRUE;
1173 e->cval = 4;
1174 break;
1175 case RPC_FC_HYPER:
1176 case RPC_FC_DOUBLE:
1177 e->is_const = TRUE;
1178 e->cval = 8;
1179 break;
1182 if (type == EXPR_CAST && expr->is_const) {
1183 e->is_const = TRUE;
1184 e->cval = expr->cval;
1186 return e;
1189 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1191 expr_t *e;
1192 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1193 error_loc("address-of operator applied to invalid expression\n");
1194 e = xmalloc(sizeof(expr_t));
1195 e->type = type;
1196 e->ref = expr;
1197 e->u.lval = 0;
1198 e->is_const = FALSE;
1199 /* check for compile-time optimization */
1200 if (expr->is_const) {
1201 e->is_const = TRUE;
1202 switch (type) {
1203 case EXPR_NEG:
1204 e->cval = -expr->cval;
1205 break;
1206 case EXPR_NOT:
1207 e->cval = ~expr->cval;
1208 break;
1209 default:
1210 e->is_const = FALSE;
1211 break;
1214 return e;
1217 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1219 expr_t *e;
1220 e = xmalloc(sizeof(expr_t));
1221 e->type = type;
1222 e->ref = expr1;
1223 e->u.ext = expr2;
1224 e->is_const = FALSE;
1225 /* check for compile-time optimization */
1226 if (expr1->is_const && expr2->is_const) {
1227 e->is_const = TRUE;
1228 switch (type) {
1229 case EXPR_ADD:
1230 e->cval = expr1->cval + expr2->cval;
1231 break;
1232 case EXPR_SUB:
1233 e->cval = expr1->cval - expr2->cval;
1234 break;
1235 case EXPR_MUL:
1236 e->cval = expr1->cval * expr2->cval;
1237 break;
1238 case EXPR_DIV:
1239 e->cval = expr1->cval / expr2->cval;
1240 break;
1241 case EXPR_OR:
1242 e->cval = expr1->cval | expr2->cval;
1243 break;
1244 case EXPR_AND:
1245 e->cval = expr1->cval & expr2->cval;
1246 break;
1247 case EXPR_SHL:
1248 e->cval = expr1->cval << expr2->cval;
1249 break;
1250 case EXPR_SHR:
1251 e->cval = expr1->cval >> expr2->cval;
1252 break;
1253 default:
1254 e->is_const = FALSE;
1255 break;
1258 return e;
1261 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1263 expr_t *e;
1264 e = xmalloc(sizeof(expr_t));
1265 e->type = type;
1266 e->ref = expr1;
1267 e->u.ext = expr2;
1268 e->ext2 = expr3;
1269 e->is_const = FALSE;
1270 /* check for compile-time optimization */
1271 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1272 e->is_const = TRUE;
1273 switch (type) {
1274 case EXPR_COND:
1275 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1276 break;
1277 default:
1278 e->is_const = FALSE;
1279 break;
1282 return e;
1285 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1287 if (!expr) return list;
1288 if (!list)
1290 list = xmalloc( sizeof(*list) );
1291 list_init( list );
1293 list_add_tail( list, &expr->entry );
1294 return list;
1297 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1299 if (!expr) return list;
1300 if (!list)
1302 list = xmalloc( sizeof(*list) );
1303 list_init( list );
1305 list_add_tail( list, &expr->entry );
1306 return list;
1309 static struct list type_pool = LIST_INIT(type_pool);
1310 typedef struct
1312 type_t data;
1313 struct list link;
1314 } type_pool_node_t;
1316 type_t *alloc_type(void)
1318 type_pool_node_t *node = xmalloc(sizeof *node);
1319 list_add_tail(&type_pool, &node->link);
1320 return &node->data;
1323 void set_all_tfswrite(int val)
1325 type_pool_node_t *node;
1326 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1327 node->data.tfswrite = val;
1330 static type_t *make_type(unsigned char type, type_t *ref)
1332 type_t *t = alloc_type();
1333 t->name = NULL;
1334 t->kind = TKIND_PRIMITIVE;
1335 t->type = type;
1336 t->ref = ref;
1337 t->attrs = NULL;
1338 t->orig = NULL;
1339 t->funcs = NULL;
1340 t->fields_or_args = NULL;
1341 t->ifaces = NULL;
1342 t->dim = 0;
1343 t->size_is = NULL;
1344 t->length_is = NULL;
1345 t->typestring_offset = 0;
1346 t->ptrdesc = 0;
1347 t->declarray = FALSE;
1348 t->ignore = (parse_only != 0);
1349 t->is_const = FALSE;
1350 t->sign = 0;
1351 t->defined = FALSE;
1352 t->written = FALSE;
1353 t->user_types_registered = FALSE;
1354 t->tfswrite = FALSE;
1355 t->typelib_idx = -1;
1356 return t;
1359 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr,
1360 int top)
1362 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1363 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1364 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1365 int ptr_type = ptr_attr;
1366 int sizeless, has_varconf;
1367 expr_t *dim;
1368 type_t *atype, **ptype;
1369 int ptr_level = (pident ? pident->ptr_level : 0);
1371 v->type = type;
1373 if (!ptr_type && top)
1374 ptr_type = RPC_FC_RP;
1376 for ( ; 0 < ptr_level; --ptr_level)
1378 v->type = make_type(pointer_default, v->type);
1379 if (ptr_level == 1 && ptr_type && !arr)
1381 v->type->type = ptr_type;
1382 ptr_type = 0;
1386 if (ptr_type && !arr)
1388 if (is_ptr(v->type))
1390 if (v->type->type != ptr_type)
1392 v->type = duptype(v->type, 1);
1393 v->type->type = ptr_type;
1396 else if (!arr && ptr_attr)
1397 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1400 if (pident && pident->is_func) {
1401 int func_ptr_level = pident->func_ptr_level;
1402 v->type = make_type(RPC_FC_FUNCTION, v->type);
1403 v->type->fields_or_args = pident->args;
1404 if (pident->callconv)
1405 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1406 else if (is_object_interface) {
1407 static char *stdmethodcalltype;
1408 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1409 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1411 for (; func_ptr_level > 0; func_ptr_level--)
1412 v->type = make_type(ptr_type, v->type);
1415 sizeless = FALSE;
1416 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1418 if (sizeless)
1419 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1421 if (dim->is_const)
1423 unsigned int align = 0;
1424 size_t size = type_memsize(v->type, &align);
1426 if (dim->cval <= 0)
1427 error_loc("%s: array dimension must be positive\n", v->name);
1429 if (0xffffffffuL / size < (unsigned long) dim->cval)
1430 error_loc("%s: total array size is too large\n", v->name);
1431 else if (0xffffuL < size * dim->cval)
1432 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1433 else
1434 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1436 else
1438 sizeless = TRUE;
1439 v->type = make_type(RPC_FC_CARRAY, v->type);
1442 v->type->declarray = TRUE;
1443 v->type->dim = dim->cval;
1446 ptype = &v->type;
1447 has_varconf = FALSE;
1448 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1450 if (dim->type != EXPR_VOID)
1452 has_varconf = TRUE;
1453 atype = *ptype = duptype(*ptype, 0);
1455 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1456 error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1458 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1459 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1461 atype->type = RPC_FC_CARRAY;
1462 atype->size_is = dim;
1465 ptype = &(*ptype)->ref;
1466 if (*ptype == NULL)
1467 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1470 ptype = &v->type;
1471 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1473 if (dim->type != EXPR_VOID)
1475 has_varconf = TRUE;
1476 atype = *ptype = duptype(*ptype, 0);
1478 if (atype->type == RPC_FC_SMFARRAY)
1479 atype->type = RPC_FC_SMVARRAY;
1480 else if (atype->type == RPC_FC_LGFARRAY)
1481 atype->type = RPC_FC_LGVARRAY;
1482 else if (atype->type == RPC_FC_CARRAY)
1483 atype->type = RPC_FC_CVARRAY;
1484 else
1485 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1487 atype->length_is = dim;
1490 ptype = &(*ptype)->ref;
1491 if (*ptype == NULL)
1492 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1495 if (has_varconf && !last_array(v->type))
1497 ptype = &v->type;
1498 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1500 *ptype = duptype(*ptype, 0);
1501 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1505 if (is_array(v->type))
1507 const type_t *rt = v->type->ref;
1508 if (is_user_type(rt))
1509 v->type->type = RPC_FC_BOGUS_ARRAY;
1510 else
1511 switch (rt->type)
1513 case RPC_FC_BOGUS_STRUCT:
1514 case RPC_FC_NON_ENCAPSULATED_UNION:
1515 case RPC_FC_ENCAPSULATED_UNION:
1516 case RPC_FC_ENUM16:
1517 v->type->type = RPC_FC_BOGUS_ARRAY;
1518 break;
1519 /* FC_RP should be above, but widl overuses these, and will break things. */
1520 case RPC_FC_UP:
1521 case RPC_FC_RP:
1522 if (rt->ref->type == RPC_FC_IP)
1523 v->type->type = RPC_FC_BOGUS_ARRAY;
1524 break;
1529 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1531 if (!iface) return list;
1532 if (!list)
1534 list = xmalloc( sizeof(*list) );
1535 list_init( list );
1537 list_add_tail( list, &iface->entry );
1538 return list;
1541 static ifref_t *make_ifref(type_t *iface)
1543 ifref_t *l = xmalloc(sizeof(ifref_t));
1544 l->iface = iface;
1545 l->attrs = NULL;
1546 return l;
1549 static var_list_t *append_var(var_list_t *list, var_t *var)
1551 if (!var) return list;
1552 if (!list)
1554 list = xmalloc( sizeof(*list) );
1555 list_init( list );
1557 list_add_tail( list, &var->entry );
1558 return list;
1561 static var_t *make_var(char *name)
1563 var_t *v = xmalloc(sizeof(var_t));
1564 v->name = name;
1565 v->type = NULL;
1566 v->attrs = NULL;
1567 v->eval = NULL;
1568 v->loc_info.input_name = input_name ? input_name : "stdin";
1569 v->loc_info.line_number = line_number;
1570 v->loc_info.near_text = parser_text;
1571 return v;
1574 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1576 if (!p) return list;
1577 if (!list) {
1578 list = xmalloc(sizeof(*list));
1579 list_init(list);
1581 list_add_tail(list, &p->entry);
1582 return list;
1585 static pident_t *make_pident(var_t *var)
1587 pident_t *p = xmalloc(sizeof(*p));
1588 p->var = var;
1589 p->is_func = FALSE;
1590 p->ptr_level = 0;
1591 p->func_ptr_level = 0;
1592 p->args = NULL;
1593 p->callconv = NULL;
1594 return p;
1597 static func_list_t *append_func(func_list_t *list, func_t *func)
1599 if (!func) return list;
1600 if (!list)
1602 list = xmalloc( sizeof(*list) );
1603 list_init( list );
1605 list_add_tail( list, &func->entry );
1606 return list;
1609 static func_t *make_func(var_t *def, var_list_t *args)
1611 func_t *f = xmalloc(sizeof(func_t));
1612 f->def = def;
1613 f->args = args;
1614 f->ignore = parse_only;
1615 f->idx = -1;
1616 return f;
1619 static type_t *make_class(char *name)
1621 type_t *c = make_type(0, NULL);
1622 c->name = name;
1623 c->kind = TKIND_COCLASS;
1624 return c;
1627 static type_t *make_safearray(type_t *type)
1629 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1630 sa->ref = type;
1631 return make_type(pointer_default, sa);
1634 #define HASHMAX 64
1636 static int hash_ident(const char *name)
1638 const char *p = name;
1639 int sum = 0;
1640 /* a simple sum hash is probably good enough */
1641 while (*p) {
1642 sum += *p;
1643 p++;
1645 return sum & (HASHMAX-1);
1648 /***** type repository *****/
1650 struct rtype {
1651 const char *name;
1652 type_t *type;
1653 int t;
1654 struct rtype *next;
1657 struct rtype *type_hash[HASHMAX];
1659 static type_t *reg_type(type_t *type, const char *name, int t)
1661 struct rtype *nt;
1662 int hash;
1663 if (!name) {
1664 error_loc("registering named type without name\n");
1665 return type;
1667 hash = hash_ident(name);
1668 nt = xmalloc(sizeof(struct rtype));
1669 nt->name = name;
1670 nt->type = type;
1671 nt->t = t;
1672 nt->next = type_hash[hash];
1673 type_hash[hash] = nt;
1674 return type;
1677 static int is_incomplete(const type_t *t)
1679 return !t->defined && (is_struct(t->type) || is_union(t->type));
1682 static void add_incomplete(type_t *t)
1684 struct typenode *tn = xmalloc(sizeof *tn);
1685 tn->type = t;
1686 list_add_tail(&incomplete_types, &tn->entry);
1689 static void fix_type(type_t *t)
1691 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1692 type_t *ot = t->orig;
1693 fix_type(ot);
1694 t->fields_or_args = ot->fields_or_args;
1695 t->defined = ot->defined;
1699 static void fix_incomplete(void)
1701 struct typenode *tn, *next;
1703 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1704 fix_type(tn->type);
1705 free(tn);
1709 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1711 type_t *ptr = type;
1712 const pident_t *pident;
1713 int ptrc = 0;
1714 int is_str = is_attr(attrs, ATTR_STRING);
1715 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1717 if (is_str)
1719 type_t *t = type;
1720 unsigned char c;
1722 while (is_ptr(t))
1723 t = t->ref;
1725 c = t->type;
1726 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1728 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1729 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1730 pident->var->name);
1734 /* We must generate names for tagless enum, struct or union.
1735 Typedef-ing a tagless enum, struct or union means we want the typedef
1736 to be included in a library whether it has other attributes or not,
1737 hence the public attribute. */
1738 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1739 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1741 if (! is_attr(attrs, ATTR_PUBLIC))
1742 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1743 type->name = gen_name();
1746 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1748 var_t *name = pident->var;
1750 if (name->name) {
1751 type_t *cur = ptr;
1752 int cptr = pident->ptr_level;
1753 if (cptr > ptrc) {
1754 while (cptr > ptrc) {
1755 cur = ptr = make_type(pointer_default, cur);
1756 ptrc++;
1758 } else {
1759 while (cptr < ptrc) {
1760 cur = cur->ref;
1761 cptr++;
1764 if (pident->is_func) {
1765 int func_ptr_level = pident->func_ptr_level;
1766 cur = make_type(RPC_FC_FUNCTION, cur);
1767 cur->fields_or_args = pident->args;
1768 if (pident->callconv)
1769 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1770 else if (is_object_interface) {
1771 static char *stdmethodcalltype;
1772 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1773 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1775 for (; func_ptr_level > 0; func_ptr_level--)
1776 cur = make_type(pointer_default, cur);
1778 cur = alias(cur, name->name);
1779 cur->attrs = attrs;
1780 if (ptr_type)
1782 if (is_ptr(cur))
1783 cur->type = ptr_type;
1784 else
1785 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1786 cur->name);
1788 else if (is_str && ! is_ptr(cur))
1789 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1790 cur->name);
1792 if (is_incomplete(cur))
1793 add_incomplete(cur);
1794 reg_type(cur, cur->name, 0);
1797 return type;
1800 static type_t *find_type(const char *name, int t)
1802 struct rtype *cur = type_hash[hash_ident(name)];
1803 while (cur && (cur->t != t || strcmp(cur->name, name)))
1804 cur = cur->next;
1805 if (!cur) {
1806 error_loc("type '%s' not found\n", name);
1807 return NULL;
1809 return cur->type;
1812 static type_t *find_type2(char *name, int t)
1814 type_t *tp = find_type(name, t);
1815 free(name);
1816 return tp;
1819 int is_type(const char *name)
1821 struct rtype *cur = type_hash[hash_ident(name)];
1822 while (cur && (cur->t || strcmp(cur->name, name)))
1823 cur = cur->next;
1824 if (cur) return TRUE;
1825 return FALSE;
1828 static type_t *get_type(unsigned char type, char *name, int t)
1830 struct rtype *cur = NULL;
1831 type_t *tp;
1832 if (name) {
1833 cur = type_hash[hash_ident(name)];
1834 while (cur && (cur->t != t || strcmp(cur->name, name)))
1835 cur = cur->next;
1837 if (cur) {
1838 free(name);
1839 return cur->type;
1841 tp = make_type(type, NULL);
1842 tp->name = name;
1843 if (!name) return tp;
1844 return reg_type(tp, name, t);
1847 static type_t *get_typev(unsigned char type, var_t *name, int t)
1849 char *sname = NULL;
1850 if (name) {
1851 sname = name->name;
1852 free(name);
1854 return get_type(type, sname, t);
1857 static int get_struct_type(var_list_t *fields)
1859 int has_pointer = 0;
1860 int has_conformance = 0;
1861 int has_variance = 0;
1862 var_t *field;
1864 if (get_padding(fields))
1865 return RPC_FC_BOGUS_STRUCT;
1867 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1869 type_t *t = field->type;
1871 if (is_user_type(t))
1872 return RPC_FC_BOGUS_STRUCT;
1874 if (is_ptr(t))
1877 t = t->ref;
1878 while (is_ptr(t));
1880 switch (t->type)
1882 case RPC_FC_IP:
1883 case RPC_FC_ENCAPSULATED_UNION:
1884 case RPC_FC_NON_ENCAPSULATED_UNION:
1885 case RPC_FC_BOGUS_STRUCT:
1886 return RPC_FC_BOGUS_STRUCT;
1889 has_pointer = 1;
1890 continue;
1893 if (field->type->declarray)
1895 if (is_string_type(field->attrs, field->type))
1897 if (is_conformant_array(field->type))
1898 has_conformance = 1;
1899 has_variance = 1;
1900 continue;
1903 if (is_array(field->type->ref))
1904 return RPC_FC_BOGUS_STRUCT;
1906 if (is_conformant_array(field->type))
1908 has_conformance = 1;
1909 if (field->type->declarray && list_next(fields, &field->entry))
1910 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1911 field->name);
1913 if (field->type->length_is)
1914 has_variance = 1;
1916 t = field->type->ref;
1919 switch (t->type)
1922 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1923 * Simple types don't effect the type of struct.
1924 * A struct containing a simple struct is still a simple struct.
1925 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1927 case 0: /* void pointer */
1928 case RPC_FC_BYTE:
1929 case RPC_FC_CHAR:
1930 case RPC_FC_SMALL:
1931 case RPC_FC_USMALL:
1932 case RPC_FC_WCHAR:
1933 case RPC_FC_SHORT:
1934 case RPC_FC_USHORT:
1935 case RPC_FC_LONG:
1936 case RPC_FC_ULONG:
1937 case RPC_FC_INT3264:
1938 case RPC_FC_UINT3264:
1939 case RPC_FC_HYPER:
1940 case RPC_FC_FLOAT:
1941 case RPC_FC_DOUBLE:
1942 case RPC_FC_STRUCT:
1943 case RPC_FC_ENUM32:
1944 break;
1946 case RPC_FC_RP:
1947 case RPC_FC_UP:
1948 case RPC_FC_FP:
1949 case RPC_FC_OP:
1950 case RPC_FC_CARRAY:
1951 case RPC_FC_CVARRAY:
1952 case RPC_FC_BOGUS_ARRAY:
1953 has_pointer = 1;
1954 break;
1957 * Propagate member attributes
1958 * a struct should be at least as complex as its member
1960 case RPC_FC_CVSTRUCT:
1961 has_conformance = 1;
1962 has_variance = 1;
1963 has_pointer = 1;
1964 break;
1966 case RPC_FC_CPSTRUCT:
1967 has_conformance = 1;
1968 if (list_next( fields, &field->entry ))
1969 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1970 field->name);
1971 has_pointer = 1;
1972 break;
1974 case RPC_FC_CSTRUCT:
1975 has_conformance = 1;
1976 if (list_next( fields, &field->entry ))
1977 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1978 field->name);
1979 break;
1981 case RPC_FC_PSTRUCT:
1982 has_pointer = 1;
1983 break;
1985 default:
1986 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1987 /* fallthru - treat it as complex */
1989 /* as soon as we see one of these these members, it's bogus... */
1990 case RPC_FC_ENCAPSULATED_UNION:
1991 case RPC_FC_NON_ENCAPSULATED_UNION:
1992 case RPC_FC_BOGUS_STRUCT:
1993 case RPC_FC_ENUM16:
1994 return RPC_FC_BOGUS_STRUCT;
1998 if( has_variance )
2000 if ( has_conformance )
2001 return RPC_FC_CVSTRUCT;
2002 else
2003 return RPC_FC_BOGUS_STRUCT;
2005 if( has_conformance && has_pointer )
2006 return RPC_FC_CPSTRUCT;
2007 if( has_conformance )
2008 return RPC_FC_CSTRUCT;
2009 if( has_pointer )
2010 return RPC_FC_PSTRUCT;
2011 return RPC_FC_STRUCT;
2014 /***** constant repository *****/
2016 struct rconst {
2017 char *name;
2018 var_t *var;
2019 struct rconst *next;
2022 struct rconst *const_hash[HASHMAX];
2024 static var_t *reg_const(var_t *var)
2026 struct rconst *nc;
2027 int hash;
2028 if (!var->name) {
2029 error_loc("registering constant without name\n");
2030 return var;
2032 hash = hash_ident(var->name);
2033 nc = xmalloc(sizeof(struct rconst));
2034 nc->name = var->name;
2035 nc->var = var;
2036 nc->next = const_hash[hash];
2037 const_hash[hash] = nc;
2038 return var;
2041 static var_t *find_const(char *name, int f)
2043 struct rconst *cur = const_hash[hash_ident(name)];
2044 while (cur && strcmp(cur->name, name))
2045 cur = cur->next;
2046 if (!cur) {
2047 if (f) error_loc("constant '%s' not found\n", name);
2048 return NULL;
2050 return cur->var;
2053 static void write_libid(const char *name, const attr_list_t *attr)
2055 const UUID *uuid = get_attrp(attr, ATTR_UUID);
2056 write_guid(idfile, "LIBID", name, uuid);
2059 static void write_clsid(type_t *cls)
2061 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2062 write_guid(idfile, "CLSID", cls->name, uuid);
2065 static void write_diid(type_t *iface)
2067 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2068 write_guid(idfile, "DIID", iface->name, uuid);
2071 static void write_iid(type_t *iface)
2073 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2074 write_guid(idfile, "IID", iface->name, uuid);
2077 static int compute_method_indexes(type_t *iface)
2079 int idx;
2080 func_t *f;
2082 if (iface->ref)
2083 idx = compute_method_indexes(iface->ref);
2084 else
2085 idx = 0;
2087 if (!iface->funcs)
2088 return idx;
2090 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2091 if (! is_callas(f->def->attrs))
2092 f->idx = idx++;
2094 return idx;
2097 static char *gen_name(void)
2099 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2100 static unsigned long n = 0;
2101 static const char *file_id;
2102 static size_t size;
2103 char *name;
2105 if (! file_id)
2107 char *dst = dup_basename(input_name, ".idl");
2108 file_id = dst;
2110 for (; *dst; ++dst)
2111 if (! isalnum((unsigned char) *dst))
2112 *dst = '_';
2114 size = sizeof format - 7 + strlen(file_id) + 8;
2117 name = xmalloc(size);
2118 sprintf(name, format, file_id, n++);
2119 return name;
2122 static void process_typedefs(pident_list_t *pidents)
2124 pident_t *pident, *next;
2126 if (!pidents) return;
2127 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2129 var_t *var = pident->var;
2130 type_t *type = find_type(var->name, 0);
2132 if (! parse_only && do_header)
2133 write_typedef(type);
2134 if (in_typelib && type->attrs)
2135 add_typelib_entry(type);
2137 free(pident);
2138 free(var);
2142 struct allowed_attr
2144 unsigned int dce_compatible : 1;
2145 unsigned int acf : 1;
2146 unsigned int on_interface : 1;
2147 unsigned int on_function : 1;
2148 unsigned int on_arg : 1;
2149 unsigned int on_type : 1;
2150 unsigned int on_field : 1;
2151 unsigned int on_library : 1;
2152 unsigned int on_dispinterface : 1;
2153 unsigned int on_module : 1;
2154 unsigned int on_coclass : 1;
2155 const char *display_name;
2158 struct allowed_attr allowed_attr[] =
2160 /* attr { D ACF I Fn ARG T Fi L DI M C <display name> } */
2161 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2162 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2163 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, "async" },
2164 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2165 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2166 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2167 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2168 /* ATTR_CALLCONV */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2169 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2170 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "context_handle" },
2171 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2172 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "default" },
2173 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2174 /* ATTR_DEFAULTVALUE_EXPR */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2175 /* ATTR_DEFAULTVALUE_STRING */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2176 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2177 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2178 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2179 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2180 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2181 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2182 /* ATTR_ENTRY_ORDINAL */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2183 /* ATTR_ENTRY_STRING */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2184 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2185 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "handle" },
2186 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpcontext" },
2187 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2188 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstring" },
2189 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstringcontext" },
2190 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2191 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2192 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, "id" },
2193 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2194 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "iid_is" },
2195 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2196 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2197 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "in" },
2198 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2199 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "length_is" },
2200 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "local" },
2201 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2202 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2203 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2204 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2205 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2206 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2207 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "optional" },
2208 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "out" },
2209 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2210 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2211 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propget" },
2212 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propput" },
2213 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2214 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "public" },
2215 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "range" },
2216 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "readonly" },
2217 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2218 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, "restricted" },
2219 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "retval" },
2220 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "size_is" },
2221 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, "source" },
2222 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2223 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, "string" },
2224 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "switch_is" },
2225 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, "switch_type" },
2226 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "transmit_as" },
2227 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, "uuid" },
2228 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "v1_enum" },
2229 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2230 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2231 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "wire_marshal" },
2234 const char *get_attr_display_name(enum attr_type type)
2236 return allowed_attr[type].display_name;
2239 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs)
2241 const attr_t *attr;
2242 if (!attrs) return attrs;
2243 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2245 if (!allowed_attr[attr->type].on_interface)
2246 error_loc("inapplicable attribute %s for interface %s\n",
2247 allowed_attr[attr->type].display_name, name);
2249 return attrs;
2252 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2254 const attr_t *attr;
2255 if (!attrs) return attrs;
2256 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2258 if (!allowed_attr[attr->type].on_function)
2259 error_loc("inapplicable attribute %s for function %s\n",
2260 allowed_attr[attr->type].display_name, name);
2262 return attrs;
2265 static void check_arg(var_t *arg)
2267 const type_t *t = arg->type;
2268 const attr_t *attr;
2270 if (t->type == 0 && ! is_var_ptr(arg))
2271 error_loc("argument '%s' has void type\n", arg->name);
2273 if (arg->attrs)
2275 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2277 if (!allowed_attr[attr->type].on_arg)
2278 error_loc("inapplicable attribute %s for argument %s\n",
2279 allowed_attr[attr->type].display_name, arg->name);
2284 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2286 const attr_t *attr;
2287 if (!attrs) return attrs;
2288 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2290 if (!allowed_attr[attr->type].on_type)
2291 error_loc("inapplicable attribute %s for typedef\n",
2292 allowed_attr[attr->type].display_name);
2294 return attrs;
2297 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2299 const attr_t *attr;
2300 if (!attrs) return attrs;
2301 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2303 if (!allowed_attr[attr->type].on_field)
2304 error_loc("inapplicable attribute %s for field %s\n",
2305 allowed_attr[attr->type].display_name, name);
2307 return attrs;
2310 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs)
2312 const attr_t *attr;
2313 if (!attrs) return attrs;
2314 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2316 if (!allowed_attr[attr->type].on_library)
2317 error_loc("inapplicable attribute %s for library %s\n",
2318 allowed_attr[attr->type].display_name, name);
2320 return attrs;
2323 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2325 const attr_t *attr;
2326 if (!attrs) return attrs;
2327 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2329 if (!allowed_attr[attr->type].on_dispinterface)
2330 error_loc("inapplicable attribute %s for dispinterface %s\n",
2331 allowed_attr[attr->type].display_name, name);
2333 return attrs;
2336 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs)
2338 const attr_t *attr;
2339 if (!attrs) return attrs;
2340 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2342 if (!allowed_attr[attr->type].on_module)
2343 error_loc("inapplicable attribute %s for module %s\n",
2344 allowed_attr[attr->type].display_name, name);
2346 return attrs;
2349 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs)
2351 const attr_t *attr;
2352 if (!attrs) return attrs;
2353 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2355 if (!allowed_attr[attr->type].on_coclass)
2356 error_loc("inapplicable attribute %s for coclass %s\n",
2357 allowed_attr[attr->type].display_name, name);
2359 return attrs;
2362 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2363 static void check_remoting_args(const func_t *func)
2365 const char *funcname = func->def->name;
2366 const var_t *arg;
2368 if (func->args) LIST_FOR_EACH_ENTRY( arg, func->args, const var_t, entry )
2370 int ptr_level = 0;
2371 const type_t *type = arg->type;
2372 int is_wire_marshal = 0;
2373 int is_context_handle = 0;
2375 /* get pointer level and fundamental type for the argument */
2376 for (;;)
2378 if (!is_wire_marshal && is_attr(type->attrs, ATTR_WIREMARSHAL))
2379 is_wire_marshal = 1;
2380 if (!is_context_handle && is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2381 is_context_handle = 1;
2382 if (type->kind == TKIND_ALIAS)
2383 type = type->orig;
2384 else if (is_ptr(type))
2386 ptr_level++;
2387 type = type->ref;
2389 else
2390 break;
2393 /* check that [out] parameters have enough pointer levels */
2394 if (is_attr(arg->attrs, ATTR_OUT))
2396 if (!is_array(type))
2398 if (!ptr_level)
2399 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2400 if (type->type == RPC_FC_IP && ptr_level == 1)
2401 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2405 if (type->type == 0 && !is_attr(arg->attrs, ATTR_IIDIS) && !is_wire_marshal && !is_context_handle)
2406 error_loc_info(&arg->loc_info, "parameter \'%s\' of function \'%s\' cannot derive from void *\n", arg->name, funcname);
2407 else if (type->type == RPC_FC_FUNCTION)
2408 error_loc_info(&arg->loc_info, "parameter \'%s\' of function \'%s\' cannot be a function pointer\n", arg->name, funcname);
2412 static void check_functions(const type_t *iface)
2414 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2416 const func_t *func;
2417 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2419 if (!is_attr(func->def->attrs, ATTR_LOCAL))
2420 check_remoting_args(func);
2425 static void check_all_user_types(ifref_list_t *ifrefs)
2427 const ifref_t *ifref;
2428 const func_t *f;
2430 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2432 const func_list_t *fs = ifref->iface->funcs;
2433 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2434 check_for_additional_prototype_types(f->args);
2438 int is_valid_uuid(const char *s)
2440 int i;
2442 for (i = 0; i < 36; ++i)
2443 if (i == 8 || i == 13 || i == 18 || i == 23)
2445 if (s[i] != '-')
2446 return FALSE;
2448 else
2449 if (!isxdigit(s[i]))
2450 return FALSE;
2452 return s[i] == '\0';