widl: Implement complex arrays.
[wine/wine64.git] / tools / widl / parser.y
blob46fb4390b9612bd8706730452a820dbd3e1ad2ec
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 typedef struct list typelist_t;
69 struct typenode {
70 type_t *type;
71 struct list entry;
74 typelist_t incomplete_types = LIST_INIT(incomplete_types);
76 static void add_incomplete(type_t *t);
77 static void fix_incomplete(void);
79 static str_list_t *append_str(str_list_t *list, char *str);
80 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
81 static attr_t *make_attr(enum attr_type type);
82 static attr_t *make_attrv(enum attr_type type, unsigned long val);
83 static attr_t *make_attrp(enum attr_type type, void *val);
84 static expr_t *make_expr(enum expr_type type);
85 static expr_t *make_exprl(enum expr_type type, long val);
86 static expr_t *make_exprd(enum expr_type type, double val);
87 static expr_t *make_exprs(enum expr_type type, char *val);
88 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
89 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
90 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
91 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
92 static type_t *make_type(unsigned char type, type_t *ref);
93 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
94 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
95 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr);
96 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
97 static ifref_t *make_ifref(type_t *iface);
98 static var_list_t *append_var(var_list_t *list, var_t *var);
99 static var_t *make_var(char *name);
100 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
101 static pident_t *make_pident(var_t *var);
102 static func_list_t *append_func(func_list_t *list, func_t *func);
103 static func_t *make_func(var_t *def, var_list_t *args);
104 static type_t *make_class(char *name);
105 static type_t *make_safearray(type_t *type);
106 static type_t *make_builtin(char *name);
107 static type_t *make_int(int sign);
109 static type_t *reg_type(type_t *type, const char *name, int t);
110 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
111 static type_t *find_type(const char *name, int t);
112 static type_t *find_type2(char *name, int t);
113 static type_t *get_type(unsigned char type, char *name, int t);
114 static type_t *get_typev(unsigned char type, var_t *name, int t);
115 static int get_struct_type(var_list_t *fields);
117 static var_t *reg_const(var_t *var);
118 static var_t *find_const(char *name, int f);
120 static void write_libid(const char *name, const attr_list_t *attr);
121 static void write_clsid(type_t *cls);
122 static void write_diid(type_t *iface);
123 static void write_iid(type_t *iface);
125 static int compute_method_indexes(type_t *iface);
126 static char *gen_name(void);
127 static void process_typedefs(var_list_t *names);
128 static void check_arg(var_t *arg);
129 static void check_all_user_types(ifref_list_t *ifaces);
131 #define tsENUM 1
132 #define tsSTRUCT 2
133 #define tsUNION 3
136 %union {
137 attr_t *attr;
138 attr_list_t *attr_list;
139 str_list_t *str_list;
140 expr_t *expr;
141 expr_list_t *expr_list;
142 array_dims_t *array_dims;
143 type_t *type;
144 var_t *var;
145 var_list_t *var_list;
146 pident_t *pident;
147 pident_list_t *pident_list;
148 func_t *func;
149 func_list_t *func_list;
150 ifref_t *ifref;
151 ifref_list_t *ifref_list;
152 char *str;
153 UUID *uuid;
154 unsigned int num;
155 double dbl;
158 %token <str> aIDENTIFIER
159 %token <str> aKNOWNTYPE
160 %token <num> aNUM aHEXNUM
161 %token <dbl> aDOUBLE
162 %token <str> aSTRING
163 %token <uuid> aUUID
164 %token aEOF
165 %token SHL SHR
166 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
167 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
168 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
169 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
170 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
171 %token tDEFAULT
172 %token tDEFAULTCOLLELEM
173 %token tDEFAULTVALUE
174 %token tDEFAULTVTABLE
175 %token tDISPLAYBIND
176 %token tDISPINTERFACE
177 %token tDLLNAME tDOUBLE tDUAL
178 %token tENDPOINT
179 %token tENTRY tENUM tERRORSTATUST
180 %token tEXPLICITHANDLE tEXTERN
181 %token tFALSE
182 %token tFLOAT
183 %token tHANDLE
184 %token tHANDLET
185 %token tHELPCONTEXT tHELPFILE
186 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
187 %token tHIDDEN
188 %token tHYPER tID tIDEMPOTENT
189 %token tIIDIS
190 %token tIMMEDIATEBIND
191 %token tIMPLICITHANDLE
192 %token tIMPORT tIMPORTLIB
193 %token tIN tINLINE
194 %token tINPUTSYNC
195 %token tINT tINT64
196 %token tINTERFACE
197 %token tLCID
198 %token tLENGTHIS tLIBRARY
199 %token tLOCAL
200 %token tLONG
201 %token tMETHODS
202 %token tMODULE
203 %token tNONBROWSABLE
204 %token tNONCREATABLE
205 %token tNONEXTENSIBLE
206 %token tOBJECT tODL tOLEAUTOMATION
207 %token tOPTIONAL
208 %token tOUT
209 %token tPOINTERDEFAULT
210 %token tPROPERTIES
211 %token tPROPGET tPROPPUT tPROPPUTREF
212 %token tPTR
213 %token tPUBLIC
214 %token tRANGE
215 %token tREADONLY tREF
216 %token tREQUESTEDIT
217 %token tRESTRICTED
218 %token tRETVAL
219 %token tSAFEARRAY
220 %token tSHORT
221 %token tSIGNED
222 %token tSINGLE
223 %token tSIZEIS tSIZEOF
224 %token tSMALL
225 %token tSOURCE
226 %token tSTDCALL
227 %token tSTRING tSTRUCT
228 %token tSWITCH tSWITCHIS tSWITCHTYPE
229 %token tTRANSMITAS
230 %token tTRUE
231 %token tTYPEDEF
232 %token tUNION
233 %token tUNIQUE
234 %token tUNSIGNED
235 %token tUUID
236 %token tV1ENUM
237 %token tVARARG
238 %token tVERSION
239 %token tVOID
240 %token tWCHAR tWIREMARSHAL
242 %type <attr> attribute
243 %type <attr_list> m_attributes attributes attrib_list
244 %type <str_list> str_list
245 %type <expr> m_expr expr expr_const
246 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
247 %type <array_dims> array array_list
248 %type <type> inherit interface interfacehdr interfacedef interfacedec
249 %type <type> dispinterface dispinterfacehdr dispinterfacedef
250 %type <type> module modulehdr moduledef
251 %type <type> base_type int_std
252 %type <type> enumdef structdef uniondef
253 %type <type> type
254 %type <ifref> coclass_int
255 %type <ifref_list> gbl_statements coclass_ints
256 %type <var> arg field s_field case enum constdef externdef
257 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
258 %type <var> m_ident t_ident ident
259 %type <pident> p_ident pident
260 %type <pident_list> pident_list
261 %type <func> funcdef
262 %type <func_list> int_statements dispint_meths
263 %type <type> coclass coclasshdr coclassdef
264 %type <num> pointer_type version
265 %type <str> libraryhdr
267 %left ','
268 %right '?' ':'
269 %left '|'
270 %left '&'
271 %left '-' '+'
272 %left '*' '/'
273 %left SHL SHR
274 %right '~'
275 %right CAST
276 %right PPTR
277 %right NEG
281 input: gbl_statements { fix_incomplete();
282 check_all_user_types($1);
283 write_proxies($1);
284 write_client($1);
285 write_server($1);
289 gbl_statements: { $$ = NULL; }
290 | gbl_statements interfacedec { $$ = $1; }
291 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
292 | gbl_statements coclass ';' { $$ = $1;
293 reg_type($2, $2->name, 0);
294 if (!parse_only && do_header) write_coclass_forward($2);
296 | gbl_statements coclassdef { $$ = $1;
297 add_typelib_entry($2);
298 reg_type($2, $2->name, 0);
299 if (!parse_only && do_header) write_coclass_forward($2);
301 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
302 | gbl_statements librarydef { $$ = $1; }
303 | gbl_statements statement { $$ = $1; }
306 imp_statements: {}
307 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
308 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
309 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
310 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
311 reg_type($2, $2->name, 0);
312 if (!parse_only && do_header) write_coclass_forward($2);
314 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
315 | imp_statements statement {}
316 | imp_statements importlib {}
319 int_statements: { $$ = NULL; }
320 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
321 | int_statements statement { $$ = $1; }
324 statement: ';' {}
325 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
326 | cppquote {}
327 | enumdef ';' { if (!parse_only && do_header) {
328 write_type(header, $1, FALSE, NULL);
329 fprintf(header, ";\n\n");
332 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
333 | import {}
334 | structdef ';' { if (!parse_only && do_header) {
335 write_type(header, $1, FALSE, NULL);
336 fprintf(header, ";\n\n");
339 | typedef ';' {}
340 | uniondef ';' { if (!parse_only && do_header) {
341 write_type(header, $1, FALSE, NULL);
342 fprintf(header, ";\n\n");
347 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
349 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
350 if (!do_import($2)) yychar = aEOF; }
352 import: import_start imp_statements aEOF {}
355 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
358 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
360 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
361 if (!parse_only && do_header) write_library($2, $1);
362 if (!parse_only && do_idfile) write_libid($2, $1);
365 librarydef: library_start imp_statements '}' { end_typelib(); }
368 m_args: { $$ = NULL; }
369 | args
372 no_args: tVOID { $$ = NULL; }
375 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
376 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
377 | no_args
380 /* split into two rules to get bison to resolve a tVOID conflict */
381 arg: attributes type pident array { $$ = $3->var;
382 $$->attrs = $1;
383 set_type($$, $2, $3->ptr_level, $4);
384 free($3);
386 | type pident array { $$ = $2->var;
387 set_type($$, $1, $2->ptr_level, $3);
388 free($2);
390 | attributes type pident '(' m_args ')' { $$ = $3->var;
391 $$->attrs = $1;
392 set_type($$, $2, $3->ptr_level - 1, NULL);
393 free($3);
394 $$->args = $5;
396 | type pident '(' m_args ')' { $$ = $2->var;
397 set_type($$, $1, $2->ptr_level - 1, NULL);
398 free($2);
399 $$->args = $4;
403 array: { $$ = NULL; }
404 | '[' array_list ']' { $$ = $2; }
405 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
408 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
409 | array_list ',' expr { $$ = append_array( $1, $3 ); }
410 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
413 m_attributes: { $$ = NULL; }
414 | attributes
417 attributes:
418 '[' attrib_list ']' { $$ = $2;
419 if (!$$)
420 yyerror("empty attribute lists unsupported");
424 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
425 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
426 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
429 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
430 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
433 attribute: { $$ = NULL; }
434 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
435 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
436 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
437 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
438 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
439 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
440 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
441 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
442 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
443 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
444 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
445 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
446 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
447 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
448 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
449 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
450 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
451 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
452 | tDUAL { $$ = make_attr(ATTR_DUAL); }
453 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
454 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
455 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
456 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
457 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
458 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
459 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
460 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
461 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
462 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
463 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
464 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
465 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
466 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
467 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
468 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
469 | tIN { $$ = make_attr(ATTR_IN); }
470 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
471 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
472 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
473 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
474 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
475 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
476 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
477 | tODL { $$ = make_attr(ATTR_ODL); }
478 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
479 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
480 | tOUT { $$ = make_attr(ATTR_OUT); }
481 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
482 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
483 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
484 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
485 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
486 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
487 list = append_expr( list, $5 );
488 $$ = make_attrp(ATTR_RANGE, list); }
489 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
490 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
491 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
492 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
493 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
494 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
495 | tSTRING { $$ = make_attr(ATTR_STRING); }
496 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
497 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
498 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
499 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
500 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
501 | tVARARG { $$ = make_attr(ATTR_VARARG); }
502 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
503 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
504 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
507 callconv:
508 | tSTDCALL
511 cases: { $$ = NULL; }
512 | cases case { $$ = append_var( $1, $2 ); }
515 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
516 $$ = $4; if (!$$) $$ = make_var(NULL);
517 $$->attrs = append_attr( $$->attrs, a );
519 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
520 $$ = $3; if (!$$) $$ = make_var(NULL);
521 $$->attrs = append_attr( $$->attrs, a );
525 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
526 set_type($$, $2, 0, NULL);
527 $$->eval = $5;
531 enums: { $$ = NULL; }
532 | enum_list ',' { $$ = $1; }
533 | enum_list
536 enum_list: enum { if (!$1->eval)
537 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
538 $$ = append_var( NULL, $1 );
540 | enum_list ',' enum { if (!$3->eval)
542 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
543 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
545 $$ = append_var( $1, $3 );
549 enum: ident '=' expr_const { $$ = reg_const($1);
550 $$->eval = $3;
551 $$->type = make_int(0);
553 | ident { $$ = reg_const($1);
554 $$->type = make_int(0);
558 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
559 $$->kind = TKIND_ENUM;
560 $$->fields = $4;
561 $$->defined = TRUE;
562 if(in_typelib)
563 add_typelib_entry($$);
567 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
568 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
572 exprs: { $$ = make_expr(EXPR_VOID); }
573 | expr_list
576 expr_list: expr
577 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
581 m_expr: { $$ = make_expr(EXPR_VOID); }
582 | expr
585 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
586 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
587 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
588 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
589 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
590 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
591 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
592 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
593 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
594 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
595 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
596 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
597 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
598 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
599 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
600 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
601 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
602 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
603 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
604 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
605 | '(' expr ')' { $$ = $2; }
608 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
609 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
612 expr_const: expr { $$ = $1;
613 if (!$$->is_const)
614 yyerror("expression is not constant");
618 externdef: tEXTERN tCONST type ident { $$ = $4;
619 set_type($$, $3, 0, NULL);
623 fields: { $$ = NULL; }
624 | fields field { $$ = append_var( $1, $2 ); }
627 field: s_field ';' { $$ = $1; }
628 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
629 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
630 | ';' { $$ = NULL; }
633 s_field: m_attributes type pident array { $$ = $3->var;
634 $$->attrs = $1;
635 set_type($$, $2, $3->ptr_level, $4);
636 free($3);
640 funcdef:
641 m_attributes type callconv pident
642 '(' m_args ')' { var_t *v = $4->var;
643 v->attrs = $1;
644 set_type(v, $2, $4->ptr_level, NULL);
645 free($4);
646 $$ = make_func(v, $6);
647 if (is_attr(v->attrs, ATTR_IN)) {
648 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
653 m_ident: { $$ = NULL; }
654 | ident
657 t_ident: { $$ = NULL; }
658 | aIDENTIFIER { $$ = make_var($1); }
659 | aKNOWNTYPE { $$ = make_var($1); }
662 ident: aIDENTIFIER { $$ = make_var($1); }
663 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
664 | aKNOWNTYPE { $$ = make_var($<str>1); }
667 base_type: tBYTE { $$ = make_builtin($<str>1); }
668 | tWCHAR { $$ = make_builtin($<str>1); }
669 | int_std
670 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
671 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
672 switch ($$->type) {
673 case RPC_FC_CHAR: break;
674 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
675 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
676 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
677 case RPC_FC_HYPER:
678 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
680 $$ = alias($$, "MIDL_uhyper");
681 $$->sign = 0;
683 break;
684 default: break;
687 | tUNSIGNED { $$ = make_int(-1); }
688 | tFLOAT { $$ = make_builtin($<str>1); }
689 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
690 | tDOUBLE { $$ = make_builtin($<str>1); }
691 | tBOOLEAN { $$ = make_builtin($<str>1); }
692 | tERRORSTATUST { $$ = make_builtin($<str>1); }
693 | tHANDLET { $$ = make_builtin($<str>1); }
696 m_int:
697 | tINT
700 int_std: tINT { $$ = make_builtin($<str>1); }
701 | tSHORT m_int { $$ = make_builtin($<str>1); }
702 | tSMALL { $$ = make_builtin($<str>1); }
703 | tLONG m_int { $$ = make_builtin($<str>1); }
704 | tHYPER m_int { $$ = make_builtin($<str>1); }
705 | tINT64 { $$ = make_builtin($<str>1); }
706 | tCHAR { $$ = make_builtin($<str>1); }
709 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
710 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
711 if ($$->defined) yyerror("multiple definition error");
712 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
716 coclasshdr: attributes coclass { $$ = $2;
717 $$->attrs = $1;
718 if (!parse_only && do_header)
719 write_coclass($$);
720 if (!parse_only && do_idfile)
721 write_clsid($$);
725 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
726 $$->ifaces = $3;
727 $$->defined = TRUE;
731 coclass_ints: { $$ = NULL; }
732 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
735 coclass_int:
736 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
739 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
740 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
743 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
744 $$ = $2;
745 if ($$->defined) yyerror("multiple definition error");
746 attrs = make_attr(ATTR_DISPINTERFACE);
747 $$->attrs = append_attr( $1, attrs );
748 $$->ref = find_type("IDispatch", 0);
749 if (!$$->ref) yyerror("IDispatch is undefined");
750 $$->defined = TRUE;
751 if (!parse_only && do_header) write_forward($$);
755 dispint_props: tPROPERTIES ':' { $$ = NULL; }
756 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
759 dispint_meths: tMETHODS ':' { $$ = NULL; }
760 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
763 dispinterfacedef: dispinterfacehdr '{'
764 dispint_props
765 dispint_meths
766 '}' { $$ = $1;
767 $$->fields = $3;
768 $$->funcs = $4;
769 if (!parse_only && do_header) write_dispinterface($$);
770 if (!parse_only && do_idfile) write_diid($$);
772 | dispinterfacehdr
773 '{' interface ';' '}' { $$ = $1;
774 $$->fields = $3->fields;
775 $$->funcs = $3->funcs;
776 if (!parse_only && do_header) write_dispinterface($$);
777 if (!parse_only && do_idfile) write_diid($$);
781 inherit: { $$ = NULL; }
782 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
785 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
786 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
789 interfacehdr: attributes interface { $$ = $2;
790 if ($$->defined) yyerror("multiple definition error");
791 $$->attrs = $1;
792 $$->defined = TRUE;
793 if (!parse_only && do_header) write_forward($$);
797 interfacedef: interfacehdr inherit
798 '{' int_statements '}' { $$ = $1;
799 $$->ref = $2;
800 $$->funcs = $4;
801 compute_method_indexes($$);
802 if (!parse_only && do_header) write_interface($$);
803 if (!parse_only && do_idfile) write_iid($$);
805 /* MIDL is able to import the definition of a base class from inside the
806 * definition of a derived class, I'll try to support it with this rule */
807 | interfacehdr ':' aIDENTIFIER
808 '{' import int_statements '}' { $$ = $1;
809 $$->ref = find_type2($3, 0);
810 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
811 $$->funcs = $6;
812 compute_method_indexes($$);
813 if (!parse_only && do_header) write_interface($$);
814 if (!parse_only && do_idfile) write_iid($$);
816 | dispinterfacedef { $$ = $1; }
819 interfacedec:
820 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
821 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
824 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
825 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
828 modulehdr: attributes module { $$ = $2;
829 $$->attrs = $1;
833 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
834 $$->funcs = $3;
835 /* FIXME: if (!parse_only && do_header) write_module($$); */
839 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
840 | tCONST p_ident { $$ = $2; /* FIXME */ }
843 pident: ident { $$ = make_pident($1); }
844 | p_ident
845 | '(' pident ')' { $$ = $2; }
848 pident_list:
849 pident { $$ = append_pident( NULL, $1 ); }
850 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
853 pointer_type:
854 tREF { $$ = RPC_FC_RP; }
855 | tUNIQUE { $$ = RPC_FC_UP; }
856 | tPTR { $$ = RPC_FC_FP; }
859 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
860 /* overwrite RPC_FC_STRUCT with a more exact type */
861 $$->type = get_struct_type( $4 );
862 $$->kind = TKIND_RECORD;
863 $$->fields = $4;
864 $$->defined = TRUE;
865 if(in_typelib)
866 add_typelib_entry($$);
870 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
871 | aKNOWNTYPE { $$ = find_type($1, 0); }
872 | base_type { $$ = $1; }
873 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
874 | enumdef { $$ = $1; }
875 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
876 | structdef { $$ = $1; }
877 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
878 | uniondef { $$ = $1; }
879 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
880 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
883 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
884 process_typedefs($4);
888 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
889 $$->kind = TKIND_UNION;
890 $$->fields = $4;
891 $$->defined = TRUE;
893 | tUNION t_ident
894 tSWITCH '(' s_field ')'
895 m_ident '{' cases '}' { var_t *u = $7;
896 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
897 $$->kind = TKIND_UNION;
898 if (!u) u = make_var( xstrdup("tagged_union") );
899 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
900 u->type->kind = TKIND_UNION;
901 u->type->fields = $9;
902 u->type->defined = TRUE;
903 $$->fields = append_var( $$->fields, $5 );
904 $$->fields = append_var( $$->fields, u );
905 $$->defined = TRUE;
909 version:
910 aNUM { $$ = MAKELONG($1, 0); }
911 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
916 static void decl_builtin(const char *name, unsigned char type)
918 type_t *t = make_type(type, NULL);
919 t->name = xstrdup(name);
920 reg_type(t, name, 0);
923 static type_t *make_builtin(char *name)
925 /* NAME is strdup'd in the lexer */
926 type_t *t = duptype(find_type(name, 0), 0);
927 t->name = name;
928 return t;
931 static type_t *make_int(int sign)
933 type_t *t = duptype(find_type("int", 0), 1);
935 t->sign = sign;
936 if (sign < 0)
937 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
939 return t;
942 void init_types(void)
944 decl_builtin("void", 0);
945 decl_builtin("byte", RPC_FC_BYTE);
946 decl_builtin("wchar_t", RPC_FC_WCHAR);
947 decl_builtin("int", RPC_FC_LONG); /* win32 */
948 decl_builtin("short", RPC_FC_SHORT);
949 decl_builtin("small", RPC_FC_SMALL);
950 decl_builtin("long", RPC_FC_LONG);
951 decl_builtin("hyper", RPC_FC_HYPER);
952 decl_builtin("__int64", RPC_FC_HYPER);
953 decl_builtin("char", RPC_FC_CHAR);
954 decl_builtin("float", RPC_FC_FLOAT);
955 decl_builtin("double", RPC_FC_DOUBLE);
956 decl_builtin("boolean", RPC_FC_BYTE);
957 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
958 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
961 static str_list_t *append_str(str_list_t *list, char *str)
963 struct str_list_entry_t *entry;
965 if (!str) return list;
966 if (!list)
968 list = xmalloc( sizeof(*list) );
969 list_init( list );
971 entry = xmalloc( sizeof(*entry) );
972 entry->str = str;
973 list_add_tail( list, &entry->entry );
974 return list;
977 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
979 if (!attr) return list;
980 if (!list)
982 list = xmalloc( sizeof(*list) );
983 list_init( list );
985 list_add_tail( list, &attr->entry );
986 return list;
989 static attr_t *make_attr(enum attr_type type)
991 attr_t *a = xmalloc(sizeof(attr_t));
992 a->type = type;
993 a->u.ival = 0;
994 return a;
997 static attr_t *make_attrv(enum attr_type type, unsigned long val)
999 attr_t *a = xmalloc(sizeof(attr_t));
1000 a->type = type;
1001 a->u.ival = val;
1002 return a;
1005 static attr_t *make_attrp(enum attr_type type, void *val)
1007 attr_t *a = xmalloc(sizeof(attr_t));
1008 a->type = type;
1009 a->u.pval = val;
1010 return a;
1013 static expr_t *make_expr(enum expr_type type)
1015 expr_t *e = xmalloc(sizeof(expr_t));
1016 e->type = type;
1017 e->ref = NULL;
1018 e->u.lval = 0;
1019 e->is_const = FALSE;
1020 e->cval = 0;
1021 return e;
1024 static expr_t *make_exprl(enum expr_type type, long val)
1026 expr_t *e = xmalloc(sizeof(expr_t));
1027 e->type = type;
1028 e->ref = NULL;
1029 e->u.lval = val;
1030 e->is_const = FALSE;
1031 /* check for numeric constant */
1032 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1033 /* make sure true/false value is valid */
1034 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1035 e->is_const = TRUE;
1036 e->cval = val;
1038 return e;
1041 static expr_t *make_exprd(enum expr_type type, double val)
1043 expr_t *e = xmalloc(sizeof(expr_t));
1044 e->type = type;
1045 e->ref = NULL;
1046 e->u.dval = val;
1047 e->is_const = TRUE;
1048 e->cval = val;
1049 return e;
1052 static expr_t *make_exprs(enum expr_type type, char *val)
1054 expr_t *e;
1055 e = xmalloc(sizeof(expr_t));
1056 e->type = type;
1057 e->ref = NULL;
1058 e->u.sval = val;
1059 e->is_const = FALSE;
1060 /* check for predefined constants */
1061 if (type == EXPR_IDENTIFIER) {
1062 var_t *c = find_const(val, 0);
1063 if (c) {
1064 e->u.sval = c->name;
1065 free(val);
1066 e->is_const = TRUE;
1067 e->cval = c->eval->cval;
1070 return e;
1073 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1075 expr_t *e;
1076 e = xmalloc(sizeof(expr_t));
1077 e->type = type;
1078 e->ref = expr;
1079 e->u.tref = tref;
1080 e->is_const = FALSE;
1081 /* check for cast of constant expression */
1082 if (type == EXPR_SIZEOF) {
1083 switch (tref->type) {
1084 case RPC_FC_BYTE:
1085 case RPC_FC_CHAR:
1086 case RPC_FC_SMALL:
1087 case RPC_FC_USMALL:
1088 e->is_const = TRUE;
1089 e->cval = 1;
1090 break;
1091 case RPC_FC_WCHAR:
1092 case RPC_FC_USHORT:
1093 case RPC_FC_SHORT:
1094 e->is_const = TRUE;
1095 e->cval = 2;
1096 break;
1097 case RPC_FC_LONG:
1098 case RPC_FC_ULONG:
1099 case RPC_FC_FLOAT:
1100 case RPC_FC_ERROR_STATUS_T:
1101 e->is_const = TRUE;
1102 e->cval = 4;
1103 break;
1104 case RPC_FC_HYPER:
1105 case RPC_FC_DOUBLE:
1106 e->is_const = TRUE;
1107 e->cval = 8;
1108 break;
1111 if (type == EXPR_CAST && expr->is_const) {
1112 e->is_const = TRUE;
1113 e->cval = expr->cval;
1115 return e;
1118 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1120 expr_t *e;
1121 e = xmalloc(sizeof(expr_t));
1122 e->type = type;
1123 e->ref = expr;
1124 e->u.lval = 0;
1125 e->is_const = FALSE;
1126 /* check for compile-time optimization */
1127 if (expr->is_const) {
1128 e->is_const = TRUE;
1129 switch (type) {
1130 case EXPR_NEG:
1131 e->cval = -expr->cval;
1132 break;
1133 case EXPR_NOT:
1134 e->cval = ~expr->cval;
1135 break;
1136 default:
1137 e->is_const = FALSE;
1138 break;
1141 return e;
1144 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1146 expr_t *e;
1147 e = xmalloc(sizeof(expr_t));
1148 e->type = type;
1149 e->ref = expr1;
1150 e->u.ext = expr2;
1151 e->is_const = FALSE;
1152 /* check for compile-time optimization */
1153 if (expr1->is_const && expr2->is_const) {
1154 e->is_const = TRUE;
1155 switch (type) {
1156 case EXPR_ADD:
1157 e->cval = expr1->cval + expr2->cval;
1158 break;
1159 case EXPR_SUB:
1160 e->cval = expr1->cval - expr2->cval;
1161 break;
1162 case EXPR_MUL:
1163 e->cval = expr1->cval * expr2->cval;
1164 break;
1165 case EXPR_DIV:
1166 e->cval = expr1->cval / expr2->cval;
1167 break;
1168 case EXPR_OR:
1169 e->cval = expr1->cval | expr2->cval;
1170 break;
1171 case EXPR_AND:
1172 e->cval = expr1->cval & expr2->cval;
1173 break;
1174 case EXPR_SHL:
1175 e->cval = expr1->cval << expr2->cval;
1176 break;
1177 case EXPR_SHR:
1178 e->cval = expr1->cval >> expr2->cval;
1179 break;
1180 default:
1181 e->is_const = FALSE;
1182 break;
1185 return e;
1188 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1190 expr_t *e;
1191 e = xmalloc(sizeof(expr_t));
1192 e->type = type;
1193 e->ref = expr1;
1194 e->u.ext = expr2;
1195 e->ext2 = expr3;
1196 e->is_const = FALSE;
1197 /* check for compile-time optimization */
1198 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1199 e->is_const = TRUE;
1200 switch (type) {
1201 case EXPR_COND:
1202 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1203 break;
1204 default:
1205 e->is_const = FALSE;
1206 break;
1209 return e;
1212 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1214 if (!expr) return list;
1215 if (!list)
1217 list = xmalloc( sizeof(*list) );
1218 list_init( list );
1220 list_add_tail( list, &expr->entry );
1221 return list;
1224 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1226 if (!expr) return list;
1227 if (!list)
1229 list = xmalloc( sizeof(*list) );
1230 list_init( list );
1232 list_add_tail( list, &expr->entry );
1233 return list;
1236 static type_t *make_type(unsigned char type, type_t *ref)
1238 type_t *t = xmalloc(sizeof(type_t));
1239 t->name = NULL;
1240 t->kind = TKIND_PRIMITIVE;
1241 t->type = type;
1242 t->ref = ref;
1243 t->attrs = NULL;
1244 t->orig = NULL;
1245 t->funcs = NULL;
1246 t->fields = NULL;
1247 t->ifaces = NULL;
1248 t->dim = 0;
1249 t->size_is = NULL;
1250 t->length_is = NULL;
1251 t->typestring_offset = 0;
1252 t->ptrdesc = 0;
1253 t->declarray = FALSE;
1254 t->ignore = (parse_only != 0);
1255 t->is_const = FALSE;
1256 t->sign = 0;
1257 t->defined = FALSE;
1258 t->written = FALSE;
1259 t->user_types_registered = FALSE;
1260 t->tfswrite = FALSE;
1261 t->typelib_idx = -1;
1262 return t;
1265 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr)
1267 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1268 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1269 int sizeless, has_varconf;
1270 expr_t *dim;
1271 type_t *atype, **ptype;
1273 v->type = type;
1275 for ( ; 0 < ptr_level; --ptr_level)
1276 v->type = make_type(RPC_FC_RP, v->type);
1278 sizeless = FALSE;
1279 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1281 if (sizeless)
1282 error("%s: only the first array dimension can be unspecified\n", v->name);
1284 if (dim->is_const)
1286 unsigned int align = 0;
1287 size_t size = type_memsize(v->type, &align);
1289 if (dim->cval <= 0)
1290 error("%s: array dimension must be positive\n", v->name);
1292 if (0xffffffffuL / size < (unsigned long) dim->cval)
1293 error("%s: total array size is too large", v->name);
1294 else if (0xffffuL < size * dim->cval)
1295 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1296 else
1297 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1299 else
1301 sizeless = TRUE;
1302 v->type = make_type(RPC_FC_CARRAY, v->type);
1305 v->type->declarray = TRUE;
1306 v->type->dim = dim->cval;
1309 ptype = &v->type;
1310 has_varconf = FALSE;
1311 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1313 if (dim->type != EXPR_VOID)
1315 has_varconf = TRUE;
1316 atype = *ptype = duptype(*ptype, 0);
1318 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1319 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1321 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1322 error("%s: size_is attribute applied to illegal type\n", v->name);
1324 atype->type = RPC_FC_CARRAY;
1325 atype->size_is = dim;
1328 ptype = &(*ptype)->ref;
1329 if (*ptype == NULL)
1330 error("%s: too many expressions in size_is attribute\n", v->name);
1333 ptype = &v->type;
1334 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1336 if (dim->type != EXPR_VOID)
1338 has_varconf = TRUE;
1339 atype = *ptype = duptype(*ptype, 0);
1341 if (atype->type == RPC_FC_SMFARRAY)
1342 atype->type = RPC_FC_SMVARRAY;
1343 else if (atype->type == RPC_FC_LGFARRAY)
1344 atype->type = RPC_FC_LGVARRAY;
1345 else if (atype->type == RPC_FC_CARRAY)
1346 atype->type = RPC_FC_CVARRAY;
1347 else
1348 error("%s: length_is attribute applied to illegal type\n", v->name);
1350 atype->length_is = dim;
1353 ptype = &(*ptype)->ref;
1354 if (*ptype == NULL)
1355 error("%s: too many expressions in length_is attribute\n", v->name);
1358 if (has_varconf && !last_array(v->type))
1360 ptype = &v->type;
1361 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1363 *ptype = duptype(*ptype, 0);
1364 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1368 if (is_array(v->type))
1370 const type_t *rt = v->type->ref;
1371 switch (rt->type)
1373 case RPC_FC_BOGUS_STRUCT:
1374 case RPC_FC_NON_ENCAPSULATED_UNION:
1375 case RPC_FC_ENCAPSULATED_UNION:
1376 case RPC_FC_ENUM16:
1377 v->type->type = RPC_FC_BOGUS_ARRAY;
1378 break;
1379 /* FC_RP should be above, but widl overuses these, and will break things. */
1380 case RPC_FC_UP:
1381 case RPC_FC_RP:
1382 if (rt->ref->type == RPC_FC_IP)
1383 v->type->type = RPC_FC_BOGUS_ARRAY;
1384 break;
1385 default:
1386 if (is_user_type(rt))
1387 v->type->type = RPC_FC_BOGUS_ARRAY;
1392 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1394 if (!iface) return list;
1395 if (!list)
1397 list = xmalloc( sizeof(*list) );
1398 list_init( list );
1400 list_add_tail( list, &iface->entry );
1401 return list;
1404 static ifref_t *make_ifref(type_t *iface)
1406 ifref_t *l = xmalloc(sizeof(ifref_t));
1407 l->iface = iface;
1408 l->attrs = NULL;
1409 return l;
1412 static var_list_t *append_var(var_list_t *list, var_t *var)
1414 if (!var) return list;
1415 if (!list)
1417 list = xmalloc( sizeof(*list) );
1418 list_init( list );
1420 list_add_tail( list, &var->entry );
1421 return list;
1424 static var_t *make_var(char *name)
1426 var_t *v = xmalloc(sizeof(var_t));
1427 v->name = name;
1428 v->type = NULL;
1429 v->args = NULL;
1430 v->attrs = NULL;
1431 v->eval = NULL;
1432 return v;
1435 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1437 if (!p) return list;
1438 if (!list) {
1439 list = xmalloc(sizeof(*list));
1440 list_init(list);
1442 list_add_tail(list, &p->entry);
1443 return list;
1446 static pident_t *make_pident(var_t *var)
1448 pident_t *p = xmalloc(sizeof(*p));
1449 p->var = var;
1450 p->ptr_level = 0;
1451 return p;
1454 static func_list_t *append_func(func_list_t *list, func_t *func)
1456 if (!func) return list;
1457 if (!list)
1459 list = xmalloc( sizeof(*list) );
1460 list_init( list );
1462 list_add_tail( list, &func->entry );
1463 return list;
1466 static func_t *make_func(var_t *def, var_list_t *args)
1468 func_t *f = xmalloc(sizeof(func_t));
1469 f->def = def;
1470 f->args = args;
1471 f->ignore = parse_only;
1472 f->idx = -1;
1473 return f;
1476 static type_t *make_class(char *name)
1478 type_t *c = make_type(0, NULL);
1479 c->name = name;
1480 c->kind = TKIND_COCLASS;
1481 return c;
1484 static type_t *make_safearray(type_t *type)
1486 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1487 sa->ref = type;
1488 return make_type(RPC_FC_FP, sa);
1491 #define HASHMAX 64
1493 static int hash_ident(const char *name)
1495 const char *p = name;
1496 int sum = 0;
1497 /* a simple sum hash is probably good enough */
1498 while (*p) {
1499 sum += *p;
1500 p++;
1502 return sum & (HASHMAX-1);
1505 /***** type repository *****/
1507 struct rtype {
1508 const char *name;
1509 type_t *type;
1510 int t;
1511 struct rtype *next;
1514 struct rtype *type_hash[HASHMAX];
1516 static type_t *reg_type(type_t *type, const char *name, int t)
1518 struct rtype *nt;
1519 int hash;
1520 if (!name) {
1521 yyerror("registering named type without name");
1522 return type;
1524 hash = hash_ident(name);
1525 nt = xmalloc(sizeof(struct rtype));
1526 nt->name = name;
1527 nt->type = type;
1528 nt->t = t;
1529 nt->next = type_hash[hash];
1530 type_hash[hash] = nt;
1531 return type;
1534 static int is_incomplete(const type_t *t)
1536 return !t->defined && (is_struct(t->type) || is_union(t->type));
1539 static void add_incomplete(type_t *t)
1541 struct typenode *tn = xmalloc(sizeof *tn);
1542 tn->type = t;
1543 list_add_tail(&incomplete_types, &tn->entry);
1546 static void fix_type(type_t *t)
1548 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1549 type_t *ot = t->orig;
1550 fix_type(ot);
1551 t->fields = ot->fields;
1552 t->defined = ot->defined;
1556 static void fix_incomplete(void)
1558 struct typenode *tn, *next;
1560 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1561 fix_type(tn->type);
1562 free(tn);
1566 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1568 type_t *ptr = type;
1569 const pident_t *pident;
1570 int ptrc = 0;
1571 int is_str = is_attr(attrs, ATTR_STRING);
1572 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1574 if (is_str)
1576 type_t *t = type;
1577 unsigned char c;
1579 while (is_ptr(t))
1580 t = t->ref;
1582 c = t->type;
1583 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1585 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1586 yyerror("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays",
1587 pident->var->name);
1591 /* We must generate names for tagless enum, struct or union.
1592 Typedef-ing a tagless enum, struct or union means we want the typedef
1593 to be included in a library whether it has other attributes or not,
1594 hence the public attribute. */
1595 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1596 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1598 if (! is_attr(attrs, ATTR_PUBLIC))
1599 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1600 type->name = gen_name();
1603 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1605 var_t *name = pident->var;
1607 if (name->name) {
1608 type_t *cur = ptr;
1609 int cptr = pident->ptr_level;
1610 if (cptr > ptrc) {
1611 while (cptr > ptrc) {
1612 cur = ptr = make_type(RPC_FC_RP, cur);
1613 ptrc++;
1615 } else {
1616 while (cptr < ptrc) {
1617 cur = cur->ref;
1618 cptr++;
1621 cur = alias(cur, name->name);
1622 cur->attrs = attrs;
1623 if (ptr_type)
1625 if (is_ptr(cur))
1626 cur->type = ptr_type;
1627 else
1628 yyerror("'%s': pointer attribute applied to non-pointer type",
1629 cur->name);
1631 else if (is_str && ! is_ptr(cur))
1632 yyerror("'%s': [string] attribute applied to non-pointer type",
1633 cur->name);
1635 if (is_incomplete(cur))
1636 add_incomplete(cur);
1637 reg_type(cur, cur->name, 0);
1640 return type;
1643 static type_t *find_type(const char *name, int t)
1645 struct rtype *cur = type_hash[hash_ident(name)];
1646 while (cur && (cur->t != t || strcmp(cur->name, name)))
1647 cur = cur->next;
1648 if (!cur) {
1649 yyerror("type '%s' not found", name);
1650 return NULL;
1652 return cur->type;
1655 static type_t *find_type2(char *name, int t)
1657 type_t *tp = find_type(name, t);
1658 free(name);
1659 return tp;
1662 int is_type(const char *name)
1664 struct rtype *cur = type_hash[hash_ident(name)];
1665 while (cur && (cur->t || strcmp(cur->name, name)))
1666 cur = cur->next;
1667 if (cur) return TRUE;
1668 return FALSE;
1671 static type_t *get_type(unsigned char type, char *name, int t)
1673 struct rtype *cur = NULL;
1674 type_t *tp;
1675 if (name) {
1676 cur = type_hash[hash_ident(name)];
1677 while (cur && (cur->t != t || strcmp(cur->name, name)))
1678 cur = cur->next;
1680 if (cur) {
1681 free(name);
1682 return cur->type;
1684 tp = make_type(type, NULL);
1685 tp->name = name;
1686 if (!name) return tp;
1687 return reg_type(tp, name, t);
1690 static type_t *get_typev(unsigned char type, var_t *name, int t)
1692 char *sname = NULL;
1693 if (name) {
1694 sname = name->name;
1695 free(name);
1697 return get_type(type, sname, t);
1700 static int get_struct_type(var_list_t *fields)
1702 int has_pointer = 0;
1703 int has_conformance = 0;
1704 int has_variance = 0;
1705 var_t *field;
1707 if (get_padding(fields))
1708 return RPC_FC_BOGUS_STRUCT;
1710 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1712 type_t *t = field->type;
1714 if (is_ptr(t))
1717 t = t->ref;
1718 while (is_ptr(t));
1720 switch (t->type)
1722 case RPC_FC_IP:
1723 case RPC_FC_ENCAPSULATED_UNION:
1724 case RPC_FC_NON_ENCAPSULATED_UNION:
1725 case RPC_FC_BOGUS_STRUCT:
1726 return RPC_FC_BOGUS_STRUCT;
1729 has_pointer = 1;
1730 continue;
1733 if (field->type->declarray)
1735 if (is_string_type(field->attrs, field->type))
1737 if (is_conformant_array(field->type))
1738 has_conformance = 1;
1739 has_variance = 1;
1740 continue;
1743 if (is_array(field->type->ref))
1744 return RPC_FC_BOGUS_STRUCT;
1746 if (is_conformant_array(field->type))
1748 has_conformance = 1;
1749 if (field->type->declarray && list_next(fields, &field->entry))
1750 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1751 field->name);
1753 if (field->type->length_is)
1754 has_variance = 1;
1756 t = field->type->ref;
1759 switch (t->type)
1762 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1763 * Simple types don't effect the type of struct.
1764 * A struct containing a simple struct is still a simple struct.
1765 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1767 case 0: /* void pointer */
1768 case RPC_FC_BYTE:
1769 case RPC_FC_CHAR:
1770 case RPC_FC_SMALL:
1771 case RPC_FC_USMALL:
1772 case RPC_FC_WCHAR:
1773 case RPC_FC_SHORT:
1774 case RPC_FC_USHORT:
1775 case RPC_FC_LONG:
1776 case RPC_FC_ULONG:
1777 case RPC_FC_INT3264:
1778 case RPC_FC_UINT3264:
1779 case RPC_FC_HYPER:
1780 case RPC_FC_FLOAT:
1781 case RPC_FC_DOUBLE:
1782 case RPC_FC_STRUCT:
1783 case RPC_FC_ENUM16:
1784 case RPC_FC_ENUM32:
1785 break;
1787 case RPC_FC_RP:
1788 case RPC_FC_UP:
1789 case RPC_FC_FP:
1790 case RPC_FC_OP:
1791 case RPC_FC_CARRAY:
1792 case RPC_FC_CVARRAY:
1793 has_pointer = 1;
1794 break;
1797 * Propagate member attributes
1798 * a struct should be at least as complex as its member
1800 case RPC_FC_CVSTRUCT:
1801 has_conformance = 1;
1802 has_variance = 1;
1803 has_pointer = 1;
1804 break;
1806 case RPC_FC_CPSTRUCT:
1807 has_conformance = 1;
1808 if (list_next( fields, &field->entry ))
1809 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1810 field->name);
1811 has_pointer = 1;
1812 break;
1814 case RPC_FC_CSTRUCT:
1815 has_conformance = 1;
1816 if (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);
1819 break;
1821 case RPC_FC_PSTRUCT:
1822 has_pointer = 1;
1823 break;
1825 default:
1826 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1827 field->name, t->type);
1828 /* fallthru - treat it as complex */
1830 /* as soon as we see one of these these members, it's bogus... */
1831 case RPC_FC_IP:
1832 case RPC_FC_ENCAPSULATED_UNION:
1833 case RPC_FC_NON_ENCAPSULATED_UNION:
1834 case RPC_FC_TRANSMIT_AS:
1835 case RPC_FC_REPRESENT_AS:
1836 case RPC_FC_PAD:
1837 case RPC_FC_EMBEDDED_COMPLEX:
1838 case RPC_FC_BOGUS_STRUCT:
1839 case RPC_FC_BOGUS_ARRAY:
1840 return RPC_FC_BOGUS_STRUCT;
1844 if( has_variance )
1846 if ( has_conformance )
1847 return RPC_FC_CVSTRUCT;
1848 else
1849 return RPC_FC_BOGUS_STRUCT;
1851 if( has_conformance && has_pointer )
1852 return RPC_FC_CPSTRUCT;
1853 if( has_conformance )
1854 return RPC_FC_CSTRUCT;
1855 if( has_pointer )
1856 return RPC_FC_PSTRUCT;
1857 return RPC_FC_STRUCT;
1860 /***** constant repository *****/
1862 struct rconst {
1863 char *name;
1864 var_t *var;
1865 struct rconst *next;
1868 struct rconst *const_hash[HASHMAX];
1870 static var_t *reg_const(var_t *var)
1872 struct rconst *nc;
1873 int hash;
1874 if (!var->name) {
1875 yyerror("registering constant without name");
1876 return var;
1878 hash = hash_ident(var->name);
1879 nc = xmalloc(sizeof(struct rconst));
1880 nc->name = var->name;
1881 nc->var = var;
1882 nc->next = const_hash[hash];
1883 const_hash[hash] = nc;
1884 return var;
1887 static var_t *find_const(char *name, int f)
1889 struct rconst *cur = const_hash[hash_ident(name)];
1890 while (cur && strcmp(cur->name, name))
1891 cur = cur->next;
1892 if (!cur) {
1893 if (f) yyerror("constant '%s' not found", name);
1894 return NULL;
1896 return cur->var;
1899 static void write_libid(const char *name, const attr_list_t *attr)
1901 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1902 write_guid(idfile, "LIBID", name, uuid);
1905 static void write_clsid(type_t *cls)
1907 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1908 write_guid(idfile, "CLSID", cls->name, uuid);
1911 static void write_diid(type_t *iface)
1913 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1914 write_guid(idfile, "DIID", iface->name, uuid);
1917 static void write_iid(type_t *iface)
1919 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1920 write_guid(idfile, "IID", iface->name, uuid);
1923 static int compute_method_indexes(type_t *iface)
1925 int idx;
1926 func_t *f;
1928 if (iface->ref)
1929 idx = compute_method_indexes(iface->ref);
1930 else
1931 idx = 0;
1933 if (!iface->funcs)
1934 return idx;
1936 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
1937 if (! is_callas(f->def->attrs))
1938 f->idx = idx++;
1940 return idx;
1943 static char *gen_name(void)
1945 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1946 static unsigned long n = 0;
1947 static const char *file_id;
1948 static size_t size;
1949 char *name;
1951 if (! file_id)
1953 char *dst = dup_basename(input_name, ".idl");
1954 file_id = dst;
1956 for (; *dst; ++dst)
1957 if (! isalnum((unsigned char) *dst))
1958 *dst = '_';
1960 size = sizeof format - 7 + strlen(file_id) + 8;
1963 name = xmalloc(size);
1964 sprintf(name, format, file_id, n++);
1965 return name;
1968 static void process_typedefs(pident_list_t *pidents)
1970 pident_t *pident, *next;
1972 if (!pidents) return;
1973 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
1975 var_t *var = pident->var;
1976 type_t *type = find_type(var->name, 0);
1978 if (! parse_only && do_header)
1979 write_typedef(type);
1980 if (in_typelib && type->attrs)
1981 add_typelib_entry(type);
1983 free(pident);
1984 free(var);
1988 static void check_arg(var_t *arg)
1990 type_t *t = arg->type;
1992 if (t->type == 0 && ! is_var_ptr(arg))
1993 yyerror("argument '%s' has void type", arg->name);
1996 static void check_all_user_types(ifref_list_t *ifrefs)
1998 const ifref_t *ifref;
1999 const func_t *f;
2001 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2003 const func_list_t *fs = ifref->iface->funcs;
2004 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2005 check_for_user_types(f->args);