libwine: Emulate MS linker stub for builtin dlls.
[wine/wine64.git] / tools / widl / parser.y
blob06f1f77c38afb0a478f96ccade2ebd0d41e3c01c
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 "windef.h"
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "typelib.h"
41 #include "typegen.h"
43 #if defined(YYBYACC)
44 /* Berkeley yacc (byacc) doesn't seem to know about these */
45 /* Some *BSD supplied versions do define these though */
46 # ifndef YYEMPTY
47 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
48 # endif
49 # ifndef YYLEX
50 # define YYLEX yylex()
51 # endif
53 #elif defined(YYBISON)
54 /* Bison was used for original development */
55 /* #define YYEMPTY -2 */
56 /* #define YYLEX yylex() */
58 #else
59 /* No yacc we know yet */
60 # if !defined(YYEMPTY) || !defined(YYLEX)
61 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
62 # elif defined(__GNUC__) /* gcc defines the #warning directive */
63 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
64 /* #else we just take a chance that it works... */
65 # endif
66 #endif
68 unsigned char pointer_default = RPC_FC_UP;
70 typedef struct list typelist_t;
71 struct typenode {
72 type_t *type;
73 struct list entry;
76 typelist_t incomplete_types = LIST_INIT(incomplete_types);
78 static void add_incomplete(type_t *t);
79 static void fix_incomplete(void);
81 static str_list_t *append_str(str_list_t *list, char *str);
82 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
83 static attr_t *make_attr(enum attr_type type);
84 static attr_t *make_attrv(enum attr_type type, unsigned long val);
85 static attr_t *make_attrp(enum attr_type type, void *val);
86 static expr_t *make_expr(enum expr_type type);
87 static expr_t *make_exprl(enum expr_type type, long val);
88 static expr_t *make_exprd(enum expr_type type, double val);
89 static expr_t *make_exprs(enum expr_type type, char *val);
90 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
91 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
92 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
93 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
94 static type_t *make_type(unsigned char type, type_t *ref);
95 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
96 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
97 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr, int top);
98 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
99 static ifref_t *make_ifref(type_t *iface);
100 static var_list_t *append_var(var_list_t *list, var_t *var);
101 static var_t *make_var(char *name);
102 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
103 static pident_t *make_pident(var_t *var);
104 static func_list_t *append_func(func_list_t *list, func_t *func);
105 static func_t *make_func(var_t *def, var_list_t *args);
106 static type_t *make_class(char *name);
107 static type_t *make_safearray(type_t *type);
108 static type_t *make_builtin(char *name);
109 static type_t *make_int(int sign);
111 static type_t *reg_type(type_t *type, const char *name, int t);
112 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
113 static type_t *find_type(const char *name, int t);
114 static type_t *find_type2(char *name, int t);
115 static type_t *get_type(unsigned char type, char *name, int t);
116 static type_t *get_typev(unsigned char type, var_t *name, int t);
117 static int get_struct_type(var_list_t *fields);
119 static var_t *reg_const(var_t *var);
120 static var_t *find_const(char *name, int f);
122 static void write_libid(const char *name, const attr_list_t *attr);
123 static void write_clsid(type_t *cls);
124 static void write_diid(type_t *iface);
125 static void write_iid(type_t *iface);
127 static int compute_method_indexes(type_t *iface);
128 static char *gen_name(void);
129 static void process_typedefs(var_list_t *names);
130 static void check_arg(var_t *arg);
131 static void check_all_user_types(ifref_list_t *ifaces);
133 #define tsENUM 1
134 #define tsSTRUCT 2
135 #define tsUNION 3
138 %union {
139 attr_t *attr;
140 attr_list_t *attr_list;
141 str_list_t *str_list;
142 expr_t *expr;
143 expr_list_t *expr_list;
144 array_dims_t *array_dims;
145 type_t *type;
146 var_t *var;
147 var_list_t *var_list;
148 pident_t *pident;
149 pident_list_t *pident_list;
150 func_t *func;
151 func_list_t *func_list;
152 ifref_t *ifref;
153 ifref_list_t *ifref_list;
154 char *str;
155 UUID *uuid;
156 unsigned int num;
157 double dbl;
158 interface_info_t ifinfo;
161 %token <str> aIDENTIFIER
162 %token <str> aKNOWNTYPE
163 %token <num> aNUM aHEXNUM
164 %token <dbl> aDOUBLE
165 %token <str> aSTRING
166 %token <uuid> aUUID
167 %token aEOF
168 %token SHL SHR
169 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
170 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
171 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
172 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
173 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
174 %token tDEFAULT
175 %token tDEFAULTCOLLELEM
176 %token tDEFAULTVALUE
177 %token tDEFAULTVTABLE
178 %token tDISPLAYBIND
179 %token tDISPINTERFACE
180 %token tDLLNAME tDOUBLE tDUAL
181 %token tENDPOINT
182 %token tENTRY tENUM tERRORSTATUST
183 %token tEXPLICITHANDLE tEXTERN
184 %token tFALSE
185 %token tFLOAT
186 %token tHANDLE
187 %token tHANDLET
188 %token tHELPCONTEXT tHELPFILE
189 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
190 %token tHIDDEN
191 %token tHYPER tID tIDEMPOTENT
192 %token tIIDIS
193 %token tIMMEDIATEBIND
194 %token tIMPLICITHANDLE
195 %token tIMPORT tIMPORTLIB
196 %token tIN tINLINE
197 %token tINPUTSYNC
198 %token tINT tINT64
199 %token tINTERFACE
200 %token tLCID
201 %token tLENGTHIS tLIBRARY
202 %token tLOCAL
203 %token tLONG
204 %token tMETHODS
205 %token tMODULE
206 %token tNONBROWSABLE
207 %token tNONCREATABLE
208 %token tNONEXTENSIBLE
209 %token tOBJECT tODL tOLEAUTOMATION
210 %token tOPTIONAL
211 %token tOUT
212 %token tPOINTERDEFAULT
213 %token tPROPERTIES
214 %token tPROPGET tPROPPUT tPROPPUTREF
215 %token tPTR
216 %token tPUBLIC
217 %token tRANGE
218 %token tREADONLY tREF
219 %token tREQUESTEDIT
220 %token tRESTRICTED
221 %token tRETVAL
222 %token tSAFEARRAY
223 %token tSHORT
224 %token tSIGNED
225 %token tSINGLE
226 %token tSIZEIS tSIZEOF
227 %token tSMALL
228 %token tSOURCE
229 %token tSTDCALL
230 %token tSTRING tSTRUCT
231 %token tSWITCH tSWITCHIS tSWITCHTYPE
232 %token tTRANSMITAS
233 %token tTRUE
234 %token tTYPEDEF
235 %token tUNION
236 %token tUNIQUE
237 %token tUNSIGNED
238 %token tUUID
239 %token tV1ENUM
240 %token tVARARG
241 %token tVERSION
242 %token tVOID
243 %token tWCHAR tWIREMARSHAL
245 %type <attr> attribute
246 %type <attr_list> m_attributes attributes attrib_list
247 %type <str_list> str_list
248 %type <expr> m_expr expr expr_const
249 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
250 %type <array_dims> array array_list
251 %type <ifinfo> interfacehdr
252 %type <type> inherit interface interfacedef interfacedec
253 %type <type> dispinterface dispinterfacehdr dispinterfacedef
254 %type <type> module modulehdr moduledef
255 %type <type> base_type int_std
256 %type <type> enumdef structdef uniondef
257 %type <type> type
258 %type <ifref> coclass_int
259 %type <ifref_list> gbl_statements coclass_ints
260 %type <var> arg field s_field case enum constdef externdef
261 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
262 %type <var> m_ident t_ident ident
263 %type <pident> p_ident pident
264 %type <pident_list> pident_list
265 %type <func> funcdef
266 %type <func_list> int_statements dispint_meths
267 %type <type> coclass coclasshdr coclassdef
268 %type <num> pointer_type version
269 %type <str> libraryhdr
270 %type <uuid> uuid_string
272 %left ','
273 %right '?' ':'
274 %left '|'
275 %left '&'
276 %left '-' '+'
277 %left '*' '/'
278 %left SHL SHR
279 %right '~'
280 %right CAST
281 %right PPTR
282 %right NEG
286 input: gbl_statements { fix_incomplete();
287 check_all_user_types($1);
288 write_proxies($1);
289 write_client($1);
290 write_server($1);
294 gbl_statements: { $$ = NULL; }
295 | gbl_statements interfacedec { $$ = $1; }
296 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
297 | gbl_statements coclass ';' { $$ = $1;
298 reg_type($2, $2->name, 0);
299 if (!parse_only && do_header) write_coclass_forward($2);
301 | gbl_statements coclassdef { $$ = $1;
302 add_typelib_entry($2);
303 reg_type($2, $2->name, 0);
304 if (!parse_only && do_header) write_coclass_forward($2);
306 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
307 | gbl_statements librarydef { $$ = $1; }
308 | gbl_statements statement { $$ = $1; }
311 imp_statements: {}
312 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
313 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
314 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
315 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
316 reg_type($2, $2->name, 0);
317 if (!parse_only && do_header) write_coclass_forward($2);
319 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
320 | imp_statements statement {}
321 | imp_statements importlib {}
324 int_statements: { $$ = NULL; }
325 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
326 | int_statements statement { $$ = $1; }
329 statement: ';' {}
330 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
331 | cppquote {}
332 | enumdef ';' { if (!parse_only && do_header) {
333 write_type_def_or_decl(header, $1, FALSE, NULL);
334 fprintf(header, ";\n\n");
337 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
338 | import {}
339 | structdef ';' { if (!parse_only && do_header) {
340 write_type_def_or_decl(header, $1, FALSE, NULL);
341 fprintf(header, ";\n\n");
344 | typedef ';' {}
345 | uniondef ';' { if (!parse_only && do_header) {
346 write_type_def_or_decl(header, $1, FALSE, NULL);
347 fprintf(header, ";\n\n");
352 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
354 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
355 if (!do_import($2)) yychar = aEOF; }
357 import: import_start imp_statements aEOF {}
360 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
363 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
365 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
366 if (!parse_only && do_header) write_library($2, $1);
367 if (!parse_only && do_idfile) write_libid($2, $1);
370 librarydef: library_start imp_statements '}' { end_typelib(); }
373 m_args: { $$ = NULL; }
374 | args
377 no_args: tVOID { $$ = NULL; }
380 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
381 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
382 | no_args
385 /* split into two rules to get bison to resolve a tVOID conflict */
386 arg: attributes type pident array { $$ = $3->var;
387 $$->attrs = $1;
388 set_type($$, $2, $3->ptr_level, $4, TRUE);
389 free($3);
391 | type pident array { $$ = $2->var;
392 set_type($$, $1, $2->ptr_level, $3, TRUE);
393 free($2);
395 | attributes type pident '(' m_args ')' { $$ = $3->var;
396 $$->attrs = $1;
397 set_type($$, $2, $3->ptr_level - 1, NULL, TRUE);
398 free($3);
399 $$->args = $5;
401 | type pident '(' m_args ')' { $$ = $2->var;
402 set_type($$, $1, $2->ptr_level - 1, NULL, TRUE);
403 free($2);
404 $$->args = $4;
408 array: { $$ = NULL; }
409 | '[' array_list ']' { $$ = $2; }
410 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
413 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
414 | array_list ',' expr { $$ = append_array( $1, $3 ); }
415 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
418 m_attributes: { $$ = NULL; }
419 | attributes
422 attributes:
423 '[' attrib_list ']' { $$ = $2;
424 if (!$$)
425 yyerror("empty attribute lists unsupported");
429 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
430 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
431 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
434 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
435 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
438 attribute: { $$ = NULL; }
439 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
440 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
441 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
442 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
443 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
444 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
445 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
446 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
447 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
448 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
449 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
450 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
451 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
452 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
453 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
454 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
455 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
456 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
457 | tDUAL { $$ = make_attr(ATTR_DUAL); }
458 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
459 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
460 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
461 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
462 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
463 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
464 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
465 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
466 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
467 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
468 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
469 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
470 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
471 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
472 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
473 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
474 | tIN { $$ = make_attr(ATTR_IN); }
475 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
476 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
477 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
478 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
479 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
480 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
481 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
482 | tODL { $$ = make_attr(ATTR_ODL); }
483 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
484 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
485 | tOUT { $$ = make_attr(ATTR_OUT); }
486 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
487 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
488 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
489 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
490 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
491 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
492 list = append_expr( list, $5 );
493 $$ = make_attrp(ATTR_RANGE, list); }
494 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
495 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
496 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
497 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
498 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
499 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
500 | tSTRING { $$ = make_attr(ATTR_STRING); }
501 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
502 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
503 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
504 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
505 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
506 | tVARARG { $$ = make_attr(ATTR_VARARG); }
507 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
508 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
509 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
512 uuid_string:
513 aUUID
514 | aSTRING { if (!is_valid_uuid($1))
515 yyerror("invalid UUID: %s", $1);
516 $$ = parse_uuid($1); }
518 callconv:
519 | tSTDCALL
522 cases: { $$ = NULL; }
523 | cases case { $$ = append_var( $1, $2 ); }
526 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
527 $$ = $4; if (!$$) $$ = make_var(NULL);
528 $$->attrs = append_attr( $$->attrs, a );
530 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
531 $$ = $3; if (!$$) $$ = make_var(NULL);
532 $$->attrs = append_attr( $$->attrs, a );
536 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
537 set_type($$, $2, 0, NULL, FALSE);
538 $$->eval = $5;
542 enums: { $$ = NULL; }
543 | enum_list ',' { $$ = $1; }
544 | enum_list
547 enum_list: enum { if (!$1->eval)
548 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
549 $$ = append_var( NULL, $1 );
551 | enum_list ',' enum { if (!$3->eval)
553 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
554 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
556 $$ = append_var( $1, $3 );
560 enum: ident '=' expr_const { $$ = reg_const($1);
561 $$->eval = $3;
562 $$->type = make_int(0);
564 | ident { $$ = reg_const($1);
565 $$->type = make_int(0);
569 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
570 $$->kind = TKIND_ENUM;
571 $$->fields = $4;
572 $$->defined = TRUE;
573 if(in_typelib)
574 add_typelib_entry($$);
578 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
579 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
583 exprs: { $$ = make_expr(EXPR_VOID); }
584 | expr_list
587 expr_list: expr
588 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
592 m_expr: { $$ = make_expr(EXPR_VOID); }
593 | expr
596 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
597 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
598 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
599 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
600 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
601 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
602 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
603 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
604 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
605 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
606 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
607 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
608 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
609 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
610 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
611 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
612 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
613 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
614 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
615 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
616 | '(' expr ')' { $$ = $2; }
619 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
620 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
623 expr_const: expr { $$ = $1;
624 if (!$$->is_const)
625 yyerror("expression is not constant");
629 externdef: tEXTERN tCONST type ident { $$ = $4;
630 set_type($$, $3, 0, NULL, FALSE);
634 fields: { $$ = NULL; }
635 | fields field { $$ = append_var( $1, $2 ); }
638 field: s_field ';' { $$ = $1; }
639 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
640 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
641 | ';' { $$ = NULL; }
644 s_field: m_attributes type pident array { $$ = $3->var;
645 $$->attrs = $1;
646 set_type($$, $2, $3->ptr_level, $4, FALSE);
647 free($3);
651 funcdef:
652 m_attributes type callconv pident
653 '(' m_args ')' { var_t *v = $4->var;
654 v->attrs = $1;
655 set_type(v, $2, $4->ptr_level, NULL, FALSE);
656 free($4);
657 $$ = make_func(v, $6);
658 if (is_attr(v->attrs, ATTR_IN)) {
659 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
664 m_ident: { $$ = NULL; }
665 | ident
668 t_ident: { $$ = NULL; }
669 | aIDENTIFIER { $$ = make_var($1); }
670 | aKNOWNTYPE { $$ = make_var($1); }
673 ident: aIDENTIFIER { $$ = make_var($1); }
674 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
675 | aKNOWNTYPE { $$ = make_var($<str>1); }
678 base_type: tBYTE { $$ = make_builtin($<str>1); }
679 | tWCHAR { $$ = make_builtin($<str>1); }
680 | int_std
681 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
682 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
683 switch ($$->type) {
684 case RPC_FC_CHAR: break;
685 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
686 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
687 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
688 case RPC_FC_HYPER:
689 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
691 $$ = alias($$, "MIDL_uhyper");
692 $$->sign = 0;
694 break;
695 default: break;
698 | tUNSIGNED { $$ = make_int(-1); }
699 | tFLOAT { $$ = make_builtin($<str>1); }
700 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
701 | tDOUBLE { $$ = make_builtin($<str>1); }
702 | tBOOLEAN { $$ = make_builtin($<str>1); }
703 | tERRORSTATUST { $$ = make_builtin($<str>1); }
704 | tHANDLET { $$ = make_builtin($<str>1); }
707 m_int:
708 | tINT
711 int_std: tINT { $$ = make_builtin($<str>1); }
712 | tSHORT m_int { $$ = make_builtin($<str>1); }
713 | tSMALL { $$ = make_builtin($<str>1); }
714 | tLONG m_int { $$ = make_builtin($<str>1); }
715 | tHYPER m_int { $$ = make_builtin($<str>1); }
716 | tINT64 { $$ = make_builtin($<str>1); }
717 | tCHAR { $$ = make_builtin($<str>1); }
720 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
721 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
722 if ($$->defined) yyerror("multiple definition error");
723 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
727 coclasshdr: attributes coclass { $$ = $2;
728 $$->attrs = $1;
729 if (!parse_only && do_header)
730 write_coclass($$);
731 if (!parse_only && do_idfile)
732 write_clsid($$);
736 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
737 $$->ifaces = $3;
738 $$->defined = TRUE;
742 coclass_ints: { $$ = NULL; }
743 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
746 coclass_int:
747 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
750 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
751 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
754 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
755 $$ = $2;
756 if ($$->defined) yyerror("multiple definition error");
757 attrs = make_attr(ATTR_DISPINTERFACE);
758 $$->attrs = append_attr( $1, attrs );
759 $$->ref = find_type("IDispatch", 0);
760 if (!$$->ref) yyerror("IDispatch is undefined");
761 $$->defined = TRUE;
762 if (!parse_only && do_header) write_forward($$);
766 dispint_props: tPROPERTIES ':' { $$ = NULL; }
767 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
770 dispint_meths: tMETHODS ':' { $$ = NULL; }
771 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
774 dispinterfacedef: dispinterfacehdr '{'
775 dispint_props
776 dispint_meths
777 '}' { $$ = $1;
778 $$->fields = $3;
779 $$->funcs = $4;
780 if (!parse_only && do_header) write_dispinterface($$);
781 if (!parse_only && do_idfile) write_diid($$);
783 | dispinterfacehdr
784 '{' interface ';' '}' { $$ = $1;
785 $$->fields = $3->fields;
786 $$->funcs = $3->funcs;
787 if (!parse_only && do_header) write_dispinterface($$);
788 if (!parse_only && do_idfile) write_diid($$);
792 inherit: { $$ = NULL; }
793 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
796 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
797 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
800 interfacehdr: attributes interface { $$.interface = $2;
801 $$.old_pointer_default = pointer_default;
802 if (is_attr($1, ATTR_POINTERDEFAULT))
803 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
804 if ($2->defined) yyerror("multiple definition error");
805 $2->attrs = $1;
806 $2->defined = TRUE;
807 if (!parse_only && do_header) write_forward($2);
811 interfacedef: interfacehdr inherit
812 '{' int_statements '}' { $$ = $1.interface;
813 $$->ref = $2;
814 $$->funcs = $4;
815 compute_method_indexes($$);
816 if (!parse_only && do_header) write_interface($$);
817 if (!parse_only && do_idfile) write_iid($$);
818 pointer_default = $1.old_pointer_default;
820 /* MIDL is able to import the definition of a base class from inside the
821 * definition of a derived class, I'll try to support it with this rule */
822 | interfacehdr ':' aIDENTIFIER
823 '{' import int_statements '}' { $$ = $1.interface;
824 $$->ref = find_type2($3, 0);
825 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
826 $$->funcs = $6;
827 compute_method_indexes($$);
828 if (!parse_only && do_header) write_interface($$);
829 if (!parse_only && do_idfile) write_iid($$);
830 pointer_default = $1.old_pointer_default;
832 | dispinterfacedef { $$ = $1; }
835 interfacedec:
836 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
837 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
840 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
841 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
844 modulehdr: attributes module { $$ = $2;
845 $$->attrs = $1;
849 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
850 $$->funcs = $3;
851 /* FIXME: if (!parse_only && do_header) write_module($$); */
855 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
856 | tCONST p_ident { $$ = $2; /* FIXME */ }
859 pident: ident { $$ = make_pident($1); }
860 | p_ident
861 | '(' pident ')' { $$ = $2; }
864 pident_list:
865 pident { $$ = append_pident( NULL, $1 ); }
866 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
869 pointer_type:
870 tREF { $$ = RPC_FC_RP; }
871 | tUNIQUE { $$ = RPC_FC_UP; }
872 | tPTR { $$ = RPC_FC_FP; }
875 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
876 /* overwrite RPC_FC_STRUCT with a more exact type */
877 $$->type = get_struct_type( $4 );
878 $$->kind = TKIND_RECORD;
879 $$->fields = $4;
880 $$->defined = TRUE;
881 if(in_typelib)
882 add_typelib_entry($$);
886 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
887 | aKNOWNTYPE { $$ = find_type($1, 0); }
888 | base_type { $$ = $1; }
889 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
890 | enumdef { $$ = $1; }
891 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
892 | structdef { $$ = $1; }
893 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
894 | uniondef { $$ = $1; }
895 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
896 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
899 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
900 process_typedefs($4);
904 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
905 $$->kind = TKIND_UNION;
906 $$->fields = $4;
907 $$->defined = TRUE;
909 | tUNION t_ident
910 tSWITCH '(' s_field ')'
911 m_ident '{' cases '}' { var_t *u = $7;
912 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
913 $$->kind = TKIND_UNION;
914 if (!u) u = make_var( xstrdup("tagged_union") );
915 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
916 u->type->kind = TKIND_UNION;
917 u->type->fields = $9;
918 u->type->defined = TRUE;
919 $$->fields = append_var( $$->fields, $5 );
920 $$->fields = append_var( $$->fields, u );
921 $$->defined = TRUE;
925 version:
926 aNUM { $$ = MAKELONG($1, 0); }
927 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
932 static void decl_builtin(const char *name, unsigned char type)
934 type_t *t = make_type(type, NULL);
935 t->name = xstrdup(name);
936 reg_type(t, name, 0);
939 static type_t *make_builtin(char *name)
941 /* NAME is strdup'd in the lexer */
942 type_t *t = duptype(find_type(name, 0), 0);
943 t->name = name;
944 return t;
947 static type_t *make_int(int sign)
949 type_t *t = duptype(find_type("int", 0), 1);
951 t->sign = sign;
952 if (sign < 0)
953 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
955 return t;
958 void init_types(void)
960 decl_builtin("void", 0);
961 decl_builtin("byte", RPC_FC_BYTE);
962 decl_builtin("wchar_t", RPC_FC_WCHAR);
963 decl_builtin("int", RPC_FC_LONG); /* win32 */
964 decl_builtin("short", RPC_FC_SHORT);
965 decl_builtin("small", RPC_FC_SMALL);
966 decl_builtin("long", RPC_FC_LONG);
967 decl_builtin("hyper", RPC_FC_HYPER);
968 decl_builtin("__int64", RPC_FC_HYPER);
969 decl_builtin("char", RPC_FC_CHAR);
970 decl_builtin("float", RPC_FC_FLOAT);
971 decl_builtin("double", RPC_FC_DOUBLE);
972 decl_builtin("boolean", RPC_FC_BYTE);
973 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
974 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
977 static str_list_t *append_str(str_list_t *list, char *str)
979 struct str_list_entry_t *entry;
981 if (!str) return list;
982 if (!list)
984 list = xmalloc( sizeof(*list) );
985 list_init( list );
987 entry = xmalloc( sizeof(*entry) );
988 entry->str = str;
989 list_add_tail( list, &entry->entry );
990 return list;
993 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
995 if (!attr) return list;
996 if (!list)
998 list = xmalloc( sizeof(*list) );
999 list_init( list );
1001 list_add_tail( list, &attr->entry );
1002 return list;
1005 static attr_t *make_attr(enum attr_type type)
1007 attr_t *a = xmalloc(sizeof(attr_t));
1008 a->type = type;
1009 a->u.ival = 0;
1010 return a;
1013 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1015 attr_t *a = xmalloc(sizeof(attr_t));
1016 a->type = type;
1017 a->u.ival = val;
1018 return a;
1021 static attr_t *make_attrp(enum attr_type type, void *val)
1023 attr_t *a = xmalloc(sizeof(attr_t));
1024 a->type = type;
1025 a->u.pval = val;
1026 return a;
1029 static expr_t *make_expr(enum expr_type type)
1031 expr_t *e = xmalloc(sizeof(expr_t));
1032 e->type = type;
1033 e->ref = NULL;
1034 e->u.lval = 0;
1035 e->is_const = FALSE;
1036 e->cval = 0;
1037 return e;
1040 static expr_t *make_exprl(enum expr_type type, long val)
1042 expr_t *e = xmalloc(sizeof(expr_t));
1043 e->type = type;
1044 e->ref = NULL;
1045 e->u.lval = val;
1046 e->is_const = FALSE;
1047 /* check for numeric constant */
1048 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1049 /* make sure true/false value is valid */
1050 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1051 e->is_const = TRUE;
1052 e->cval = val;
1054 return e;
1057 static expr_t *make_exprd(enum expr_type type, double val)
1059 expr_t *e = xmalloc(sizeof(expr_t));
1060 e->type = type;
1061 e->ref = NULL;
1062 e->u.dval = val;
1063 e->is_const = TRUE;
1064 e->cval = val;
1065 return e;
1068 static expr_t *make_exprs(enum expr_type type, char *val)
1070 expr_t *e;
1071 e = xmalloc(sizeof(expr_t));
1072 e->type = type;
1073 e->ref = NULL;
1074 e->u.sval = val;
1075 e->is_const = FALSE;
1076 /* check for predefined constants */
1077 if (type == EXPR_IDENTIFIER) {
1078 var_t *c = find_const(val, 0);
1079 if (c) {
1080 e->u.sval = c->name;
1081 free(val);
1082 e->is_const = TRUE;
1083 e->cval = c->eval->cval;
1086 return e;
1089 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1091 expr_t *e;
1092 e = xmalloc(sizeof(expr_t));
1093 e->type = type;
1094 e->ref = expr;
1095 e->u.tref = tref;
1096 e->is_const = FALSE;
1097 /* check for cast of constant expression */
1098 if (type == EXPR_SIZEOF) {
1099 switch (tref->type) {
1100 case RPC_FC_BYTE:
1101 case RPC_FC_CHAR:
1102 case RPC_FC_SMALL:
1103 case RPC_FC_USMALL:
1104 e->is_const = TRUE;
1105 e->cval = 1;
1106 break;
1107 case RPC_FC_WCHAR:
1108 case RPC_FC_USHORT:
1109 case RPC_FC_SHORT:
1110 e->is_const = TRUE;
1111 e->cval = 2;
1112 break;
1113 case RPC_FC_LONG:
1114 case RPC_FC_ULONG:
1115 case RPC_FC_FLOAT:
1116 case RPC_FC_ERROR_STATUS_T:
1117 e->is_const = TRUE;
1118 e->cval = 4;
1119 break;
1120 case RPC_FC_HYPER:
1121 case RPC_FC_DOUBLE:
1122 e->is_const = TRUE;
1123 e->cval = 8;
1124 break;
1127 if (type == EXPR_CAST && expr->is_const) {
1128 e->is_const = TRUE;
1129 e->cval = expr->cval;
1131 return e;
1134 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1136 expr_t *e;
1137 e = xmalloc(sizeof(expr_t));
1138 e->type = type;
1139 e->ref = expr;
1140 e->u.lval = 0;
1141 e->is_const = FALSE;
1142 /* check for compile-time optimization */
1143 if (expr->is_const) {
1144 e->is_const = TRUE;
1145 switch (type) {
1146 case EXPR_NEG:
1147 e->cval = -expr->cval;
1148 break;
1149 case EXPR_NOT:
1150 e->cval = ~expr->cval;
1151 break;
1152 default:
1153 e->is_const = FALSE;
1154 break;
1157 return e;
1160 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1162 expr_t *e;
1163 e = xmalloc(sizeof(expr_t));
1164 e->type = type;
1165 e->ref = expr1;
1166 e->u.ext = expr2;
1167 e->is_const = FALSE;
1168 /* check for compile-time optimization */
1169 if (expr1->is_const && expr2->is_const) {
1170 e->is_const = TRUE;
1171 switch (type) {
1172 case EXPR_ADD:
1173 e->cval = expr1->cval + expr2->cval;
1174 break;
1175 case EXPR_SUB:
1176 e->cval = expr1->cval - expr2->cval;
1177 break;
1178 case EXPR_MUL:
1179 e->cval = expr1->cval * expr2->cval;
1180 break;
1181 case EXPR_DIV:
1182 e->cval = expr1->cval / expr2->cval;
1183 break;
1184 case EXPR_OR:
1185 e->cval = expr1->cval | expr2->cval;
1186 break;
1187 case EXPR_AND:
1188 e->cval = expr1->cval & expr2->cval;
1189 break;
1190 case EXPR_SHL:
1191 e->cval = expr1->cval << expr2->cval;
1192 break;
1193 case EXPR_SHR:
1194 e->cval = expr1->cval >> expr2->cval;
1195 break;
1196 default:
1197 e->is_const = FALSE;
1198 break;
1201 return e;
1204 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1206 expr_t *e;
1207 e = xmalloc(sizeof(expr_t));
1208 e->type = type;
1209 e->ref = expr1;
1210 e->u.ext = expr2;
1211 e->ext2 = expr3;
1212 e->is_const = FALSE;
1213 /* check for compile-time optimization */
1214 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1215 e->is_const = TRUE;
1216 switch (type) {
1217 case EXPR_COND:
1218 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1219 break;
1220 default:
1221 e->is_const = FALSE;
1222 break;
1225 return e;
1228 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1230 if (!expr) return list;
1231 if (!list)
1233 list = xmalloc( sizeof(*list) );
1234 list_init( list );
1236 list_add_tail( list, &expr->entry );
1237 return list;
1240 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1242 if (!expr) return list;
1243 if (!list)
1245 list = xmalloc( sizeof(*list) );
1246 list_init( list );
1248 list_add_tail( list, &expr->entry );
1249 return list;
1252 static struct list type_pool = LIST_INIT(type_pool);
1253 typedef struct
1255 type_t data;
1256 struct list link;
1257 } type_pool_node_t;
1259 type_t *alloc_type(void)
1261 type_pool_node_t *node = xmalloc(sizeof *node);
1262 list_add_tail(&type_pool, &node->link);
1263 return &node->data;
1266 void set_all_tfswrite(int val)
1268 type_pool_node_t *node;
1269 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1270 node->data.tfswrite = val;
1273 static type_t *make_type(unsigned char type, type_t *ref)
1275 type_t *t = alloc_type();
1276 t->name = NULL;
1277 t->kind = TKIND_PRIMITIVE;
1278 t->type = type;
1279 t->ref = ref;
1280 t->attrs = NULL;
1281 t->orig = NULL;
1282 t->funcs = NULL;
1283 t->fields = NULL;
1284 t->ifaces = NULL;
1285 t->dim = 0;
1286 t->size_is = NULL;
1287 t->length_is = NULL;
1288 t->typestring_offset = 0;
1289 t->ptrdesc = 0;
1290 t->declarray = FALSE;
1291 t->ignore = (parse_only != 0);
1292 t->is_const = FALSE;
1293 t->sign = 0;
1294 t->defined = FALSE;
1295 t->written = FALSE;
1296 t->user_types_registered = FALSE;
1297 t->tfswrite = FALSE;
1298 t->typelib_idx = -1;
1299 return t;
1302 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr,
1303 int top)
1305 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1306 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1307 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1308 int ptr_type = ptr_attr;
1309 int sizeless, has_varconf;
1310 expr_t *dim;
1311 type_t *atype, **ptype;
1313 v->type = type;
1315 if (!ptr_type && top)
1316 ptr_type = RPC_FC_RP;
1318 for ( ; 0 < ptr_level; --ptr_level)
1320 v->type = make_type(pointer_default, v->type);
1321 if (ptr_level == 1 && ptr_type && !arr)
1323 v->type->type = ptr_type;
1324 ptr_type = 0;
1328 if (ptr_type)
1330 if (is_ptr(v->type))
1332 if (v->type->type != ptr_type)
1334 v->type = duptype(v->type, 1);
1335 v->type->type = ptr_type;
1338 else if (!arr && ptr_attr)
1339 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1342 sizeless = FALSE;
1343 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1345 if (sizeless)
1346 error("%s: only the first array dimension can be unspecified\n", v->name);
1348 if (dim->is_const)
1350 unsigned int align = 0;
1351 size_t size = type_memsize(v->type, &align);
1353 if (dim->cval <= 0)
1354 error("%s: array dimension must be positive\n", v->name);
1356 if (0xffffffffuL / size < (unsigned long) dim->cval)
1357 error("%s: total array size is too large\n", v->name);
1358 else if (0xffffuL < size * dim->cval)
1359 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1360 else
1361 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1363 else
1365 sizeless = TRUE;
1366 v->type = make_type(RPC_FC_CARRAY, v->type);
1369 v->type->declarray = TRUE;
1370 v->type->dim = dim->cval;
1373 ptype = &v->type;
1374 has_varconf = FALSE;
1375 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1377 if (dim->type != EXPR_VOID)
1379 has_varconf = TRUE;
1380 atype = *ptype = duptype(*ptype, 0);
1382 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1383 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1385 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1386 error("%s: size_is attribute applied to illegal type\n", v->name);
1388 atype->type = RPC_FC_CARRAY;
1389 atype->size_is = dim;
1392 ptype = &(*ptype)->ref;
1393 if (*ptype == NULL)
1394 error("%s: too many expressions in size_is attribute\n", v->name);
1397 ptype = &v->type;
1398 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1400 if (dim->type != EXPR_VOID)
1402 has_varconf = TRUE;
1403 atype = *ptype = duptype(*ptype, 0);
1405 if (atype->type == RPC_FC_SMFARRAY)
1406 atype->type = RPC_FC_SMVARRAY;
1407 else if (atype->type == RPC_FC_LGFARRAY)
1408 atype->type = RPC_FC_LGVARRAY;
1409 else if (atype->type == RPC_FC_CARRAY)
1410 atype->type = RPC_FC_CVARRAY;
1411 else
1412 error("%s: length_is attribute applied to illegal type\n", v->name);
1414 atype->length_is = dim;
1417 ptype = &(*ptype)->ref;
1418 if (*ptype == NULL)
1419 error("%s: too many expressions in length_is attribute\n", v->name);
1422 if (has_varconf && !last_array(v->type))
1424 ptype = &v->type;
1425 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1427 *ptype = duptype(*ptype, 0);
1428 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1432 if (is_array(v->type))
1434 const type_t *rt = v->type->ref;
1435 switch (rt->type)
1437 case RPC_FC_BOGUS_STRUCT:
1438 case RPC_FC_NON_ENCAPSULATED_UNION:
1439 case RPC_FC_ENCAPSULATED_UNION:
1440 case RPC_FC_ENUM16:
1441 v->type->type = RPC_FC_BOGUS_ARRAY;
1442 break;
1443 /* FC_RP should be above, but widl overuses these, and will break things. */
1444 case RPC_FC_UP:
1445 case RPC_FC_RP:
1446 if (rt->ref->type == RPC_FC_IP)
1447 v->type->type = RPC_FC_BOGUS_ARRAY;
1448 break;
1449 default:
1450 if (is_user_type(rt))
1451 v->type->type = RPC_FC_BOGUS_ARRAY;
1456 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1458 if (!iface) return list;
1459 if (!list)
1461 list = xmalloc( sizeof(*list) );
1462 list_init( list );
1464 list_add_tail( list, &iface->entry );
1465 return list;
1468 static ifref_t *make_ifref(type_t *iface)
1470 ifref_t *l = xmalloc(sizeof(ifref_t));
1471 l->iface = iface;
1472 l->attrs = NULL;
1473 return l;
1476 static var_list_t *append_var(var_list_t *list, var_t *var)
1478 if (!var) return list;
1479 if (!list)
1481 list = xmalloc( sizeof(*list) );
1482 list_init( list );
1484 list_add_tail( list, &var->entry );
1485 return list;
1488 static var_t *make_var(char *name)
1490 var_t *v = xmalloc(sizeof(var_t));
1491 v->name = name;
1492 v->type = NULL;
1493 v->args = NULL;
1494 v->attrs = NULL;
1495 v->eval = NULL;
1496 return v;
1499 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1501 if (!p) return list;
1502 if (!list) {
1503 list = xmalloc(sizeof(*list));
1504 list_init(list);
1506 list_add_tail(list, &p->entry);
1507 return list;
1510 static pident_t *make_pident(var_t *var)
1512 pident_t *p = xmalloc(sizeof(*p));
1513 p->var = var;
1514 p->ptr_level = 0;
1515 return p;
1518 static func_list_t *append_func(func_list_t *list, func_t *func)
1520 if (!func) return list;
1521 if (!list)
1523 list = xmalloc( sizeof(*list) );
1524 list_init( list );
1526 list_add_tail( list, &func->entry );
1527 return list;
1530 static func_t *make_func(var_t *def, var_list_t *args)
1532 func_t *f = xmalloc(sizeof(func_t));
1533 f->def = def;
1534 f->args = args;
1535 f->ignore = parse_only;
1536 f->idx = -1;
1537 return f;
1540 static type_t *make_class(char *name)
1542 type_t *c = make_type(0, NULL);
1543 c->name = name;
1544 c->kind = TKIND_COCLASS;
1545 return c;
1548 static type_t *make_safearray(type_t *type)
1550 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1551 sa->ref = type;
1552 return make_type(pointer_default, sa);
1555 #define HASHMAX 64
1557 static int hash_ident(const char *name)
1559 const char *p = name;
1560 int sum = 0;
1561 /* a simple sum hash is probably good enough */
1562 while (*p) {
1563 sum += *p;
1564 p++;
1566 return sum & (HASHMAX-1);
1569 /***** type repository *****/
1571 struct rtype {
1572 const char *name;
1573 type_t *type;
1574 int t;
1575 struct rtype *next;
1578 struct rtype *type_hash[HASHMAX];
1580 static type_t *reg_type(type_t *type, const char *name, int t)
1582 struct rtype *nt;
1583 int hash;
1584 if (!name) {
1585 yyerror("registering named type without name");
1586 return type;
1588 hash = hash_ident(name);
1589 nt = xmalloc(sizeof(struct rtype));
1590 nt->name = name;
1591 nt->type = type;
1592 nt->t = t;
1593 nt->next = type_hash[hash];
1594 type_hash[hash] = nt;
1595 return type;
1598 static int is_incomplete(const type_t *t)
1600 return !t->defined && (is_struct(t->type) || is_union(t->type));
1603 static void add_incomplete(type_t *t)
1605 struct typenode *tn = xmalloc(sizeof *tn);
1606 tn->type = t;
1607 list_add_tail(&incomplete_types, &tn->entry);
1610 static void fix_type(type_t *t)
1612 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1613 type_t *ot = t->orig;
1614 fix_type(ot);
1615 t->fields = ot->fields;
1616 t->defined = ot->defined;
1620 static void fix_incomplete(void)
1622 struct typenode *tn, *next;
1624 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1625 fix_type(tn->type);
1626 free(tn);
1630 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1632 type_t *ptr = type;
1633 const pident_t *pident;
1634 int ptrc = 0;
1635 int is_str = is_attr(attrs, ATTR_STRING);
1636 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1638 if (is_str)
1640 type_t *t = type;
1641 unsigned char c;
1643 while (is_ptr(t))
1644 t = t->ref;
1646 c = t->type;
1647 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1649 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1650 yyerror("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays",
1651 pident->var->name);
1655 /* We must generate names for tagless enum, struct or union.
1656 Typedef-ing a tagless enum, struct or union means we want the typedef
1657 to be included in a library whether it has other attributes or not,
1658 hence the public attribute. */
1659 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1660 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1662 if (! is_attr(attrs, ATTR_PUBLIC))
1663 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1664 type->name = gen_name();
1667 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1669 var_t *name = pident->var;
1671 if (name->name) {
1672 type_t *cur = ptr;
1673 int cptr = pident->ptr_level;
1674 if (cptr > ptrc) {
1675 while (cptr > ptrc) {
1676 cur = ptr = make_type(pointer_default, cur);
1677 ptrc++;
1679 } else {
1680 while (cptr < ptrc) {
1681 cur = cur->ref;
1682 cptr++;
1685 cur = alias(cur, name->name);
1686 cur->attrs = attrs;
1687 if (ptr_type)
1689 if (is_ptr(cur))
1690 cur->type = ptr_type;
1691 else
1692 yyerror("'%s': pointer attribute applied to non-pointer type",
1693 cur->name);
1695 else if (is_str && ! is_ptr(cur))
1696 yyerror("'%s': [string] attribute applied to non-pointer type",
1697 cur->name);
1699 if (is_incomplete(cur))
1700 add_incomplete(cur);
1701 reg_type(cur, cur->name, 0);
1704 return type;
1707 static type_t *find_type(const char *name, int t)
1709 struct rtype *cur = type_hash[hash_ident(name)];
1710 while (cur && (cur->t != t || strcmp(cur->name, name)))
1711 cur = cur->next;
1712 if (!cur) {
1713 yyerror("type '%s' not found", name);
1714 return NULL;
1716 return cur->type;
1719 static type_t *find_type2(char *name, int t)
1721 type_t *tp = find_type(name, t);
1722 free(name);
1723 return tp;
1726 int is_type(const char *name)
1728 struct rtype *cur = type_hash[hash_ident(name)];
1729 while (cur && (cur->t || strcmp(cur->name, name)))
1730 cur = cur->next;
1731 if (cur) return TRUE;
1732 return FALSE;
1735 static type_t *get_type(unsigned char type, char *name, int t)
1737 struct rtype *cur = NULL;
1738 type_t *tp;
1739 if (name) {
1740 cur = type_hash[hash_ident(name)];
1741 while (cur && (cur->t != t || strcmp(cur->name, name)))
1742 cur = cur->next;
1744 if (cur) {
1745 free(name);
1746 return cur->type;
1748 tp = make_type(type, NULL);
1749 tp->name = name;
1750 if (!name) return tp;
1751 return reg_type(tp, name, t);
1754 static type_t *get_typev(unsigned char type, var_t *name, int t)
1756 char *sname = NULL;
1757 if (name) {
1758 sname = name->name;
1759 free(name);
1761 return get_type(type, sname, t);
1764 static int get_struct_type(var_list_t *fields)
1766 int has_pointer = 0;
1767 int has_conformance = 0;
1768 int has_variance = 0;
1769 var_t *field;
1771 if (get_padding(fields))
1772 return RPC_FC_BOGUS_STRUCT;
1774 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1776 type_t *t = field->type;
1778 if (is_user_type(t))
1779 return RPC_FC_BOGUS_STRUCT;
1781 if (is_ptr(t))
1784 t = t->ref;
1785 while (is_ptr(t));
1787 switch (t->type)
1789 case RPC_FC_IP:
1790 case RPC_FC_ENCAPSULATED_UNION:
1791 case RPC_FC_NON_ENCAPSULATED_UNION:
1792 case RPC_FC_BOGUS_STRUCT:
1793 return RPC_FC_BOGUS_STRUCT;
1796 has_pointer = 1;
1797 continue;
1800 if (field->type->declarray)
1802 if (is_string_type(field->attrs, field->type))
1804 if (is_conformant_array(field->type))
1805 has_conformance = 1;
1806 has_variance = 1;
1807 continue;
1810 if (is_array(field->type->ref))
1811 return RPC_FC_BOGUS_STRUCT;
1813 if (is_conformant_array(field->type))
1815 has_conformance = 1;
1816 if (field->type->declarray && list_next(fields, &field->entry))
1817 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1818 field->name);
1820 if (field->type->length_is)
1821 has_variance = 1;
1823 t = field->type->ref;
1826 switch (t->type)
1829 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1830 * Simple types don't effect the type of struct.
1831 * A struct containing a simple struct is still a simple struct.
1832 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1834 case 0: /* void pointer */
1835 case RPC_FC_BYTE:
1836 case RPC_FC_CHAR:
1837 case RPC_FC_SMALL:
1838 case RPC_FC_USMALL:
1839 case RPC_FC_WCHAR:
1840 case RPC_FC_SHORT:
1841 case RPC_FC_USHORT:
1842 case RPC_FC_LONG:
1843 case RPC_FC_ULONG:
1844 case RPC_FC_INT3264:
1845 case RPC_FC_UINT3264:
1846 case RPC_FC_HYPER:
1847 case RPC_FC_FLOAT:
1848 case RPC_FC_DOUBLE:
1849 case RPC_FC_STRUCT:
1850 case RPC_FC_ENUM16:
1851 case RPC_FC_ENUM32:
1852 break;
1854 case RPC_FC_RP:
1855 case RPC_FC_UP:
1856 case RPC_FC_FP:
1857 case RPC_FC_OP:
1858 case RPC_FC_CARRAY:
1859 case RPC_FC_CVARRAY:
1860 has_pointer = 1;
1861 break;
1864 * Propagate member attributes
1865 * a struct should be at least as complex as its member
1867 case RPC_FC_CVSTRUCT:
1868 has_conformance = 1;
1869 has_variance = 1;
1870 has_pointer = 1;
1871 break;
1873 case RPC_FC_CPSTRUCT:
1874 has_conformance = 1;
1875 if (list_next( fields, &field->entry ))
1876 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1877 field->name);
1878 has_pointer = 1;
1879 break;
1881 case RPC_FC_CSTRUCT:
1882 has_conformance = 1;
1883 if (list_next( fields, &field->entry ))
1884 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1885 field->name);
1886 break;
1888 case RPC_FC_PSTRUCT:
1889 has_pointer = 1;
1890 break;
1892 default:
1893 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1894 field->name, t->type);
1895 /* fallthru - treat it as complex */
1897 /* as soon as we see one of these these members, it's bogus... */
1898 case RPC_FC_IP:
1899 case RPC_FC_ENCAPSULATED_UNION:
1900 case RPC_FC_NON_ENCAPSULATED_UNION:
1901 case RPC_FC_TRANSMIT_AS:
1902 case RPC_FC_REPRESENT_AS:
1903 case RPC_FC_PAD:
1904 case RPC_FC_EMBEDDED_COMPLEX:
1905 case RPC_FC_BOGUS_STRUCT:
1906 case RPC_FC_BOGUS_ARRAY:
1907 return RPC_FC_BOGUS_STRUCT;
1911 if( has_variance )
1913 if ( has_conformance )
1914 return RPC_FC_CVSTRUCT;
1915 else
1916 return RPC_FC_BOGUS_STRUCT;
1918 if( has_conformance && has_pointer )
1919 return RPC_FC_CPSTRUCT;
1920 if( has_conformance )
1921 return RPC_FC_CSTRUCT;
1922 if( has_pointer )
1923 return RPC_FC_PSTRUCT;
1924 return RPC_FC_STRUCT;
1927 /***** constant repository *****/
1929 struct rconst {
1930 char *name;
1931 var_t *var;
1932 struct rconst *next;
1935 struct rconst *const_hash[HASHMAX];
1937 static var_t *reg_const(var_t *var)
1939 struct rconst *nc;
1940 int hash;
1941 if (!var->name) {
1942 yyerror("registering constant without name");
1943 return var;
1945 hash = hash_ident(var->name);
1946 nc = xmalloc(sizeof(struct rconst));
1947 nc->name = var->name;
1948 nc->var = var;
1949 nc->next = const_hash[hash];
1950 const_hash[hash] = nc;
1951 return var;
1954 static var_t *find_const(char *name, int f)
1956 struct rconst *cur = const_hash[hash_ident(name)];
1957 while (cur && strcmp(cur->name, name))
1958 cur = cur->next;
1959 if (!cur) {
1960 if (f) yyerror("constant '%s' not found", name);
1961 return NULL;
1963 return cur->var;
1966 static void write_libid(const char *name, const attr_list_t *attr)
1968 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1969 write_guid(idfile, "LIBID", name, uuid);
1972 static void write_clsid(type_t *cls)
1974 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1975 write_guid(idfile, "CLSID", cls->name, uuid);
1978 static void write_diid(type_t *iface)
1980 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1981 write_guid(idfile, "DIID", iface->name, uuid);
1984 static void write_iid(type_t *iface)
1986 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1987 write_guid(idfile, "IID", iface->name, uuid);
1990 static int compute_method_indexes(type_t *iface)
1992 int idx;
1993 func_t *f;
1995 if (iface->ref)
1996 idx = compute_method_indexes(iface->ref);
1997 else
1998 idx = 0;
2000 if (!iface->funcs)
2001 return idx;
2003 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2004 if (! is_callas(f->def->attrs))
2005 f->idx = idx++;
2007 return idx;
2010 static char *gen_name(void)
2012 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2013 static unsigned long n = 0;
2014 static const char *file_id;
2015 static size_t size;
2016 char *name;
2018 if (! file_id)
2020 char *dst = dup_basename(input_name, ".idl");
2021 file_id = dst;
2023 for (; *dst; ++dst)
2024 if (! isalnum((unsigned char) *dst))
2025 *dst = '_';
2027 size = sizeof format - 7 + strlen(file_id) + 8;
2030 name = xmalloc(size);
2031 sprintf(name, format, file_id, n++);
2032 return name;
2035 static void process_typedefs(pident_list_t *pidents)
2037 pident_t *pident, *next;
2039 if (!pidents) return;
2040 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2042 var_t *var = pident->var;
2043 type_t *type = find_type(var->name, 0);
2045 if (! parse_only && do_header)
2046 write_typedef(type);
2047 if (in_typelib && type->attrs)
2048 add_typelib_entry(type);
2050 free(pident);
2051 free(var);
2055 static void check_arg(var_t *arg)
2057 type_t *t = arg->type;
2059 if (t->type == 0 && ! is_var_ptr(arg))
2060 yyerror("argument '%s' has void type", arg->name);
2063 static void check_all_user_types(ifref_list_t *ifrefs)
2065 const ifref_t *ifref;
2066 const func_t *f;
2068 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2070 const func_list_t *fs = ifref->iface->funcs;
2071 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2072 check_for_user_types_and_context_handles(f->args);
2076 int is_valid_uuid(const char *s)
2078 int i;
2080 for (i = 0; i < 36; ++i)
2081 if (i == 8 || i == 13 || i == 18 || i == 23)
2083 if (s[i] != '-')
2084 return FALSE;
2086 else
2087 if (!isxdigit(s[i]))
2088 return FALSE;
2090 return s[i] == '\0';