winex11.drv: Added a basic client-side DIB copy optimization.
[wine/multimedia.git] / tools / widl / parser.y
blob55649369923d3be2df87c3b2595048f755524b27
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"
42 #if defined(YYBYACC)
43 /* Berkeley yacc (byacc) doesn't seem to know about these */
44 /* Some *BSD supplied versions do define these though */
45 # ifndef YYEMPTY
46 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
47 # endif
48 # ifndef YYLEX
49 # define YYLEX yylex()
50 # endif
52 #elif defined(YYBISON)
53 /* Bison was used for original development */
54 /* #define YYEMPTY -2 */
55 /* #define YYLEX yylex() */
57 #else
58 /* No yacc we know yet */
59 # if !defined(YYEMPTY) || !defined(YYLEX)
60 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
61 # elif defined(__GNUC__) /* gcc defines the #warning directive */
62 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
63 /* #else we just take a chance that it works... */
64 # endif
65 #endif
67 static attr_t *make_attr(enum attr_type type);
68 static attr_t *make_attrv(enum attr_type type, unsigned long val);
69 static attr_t *make_attrp(enum attr_type type, void *val);
70 static expr_t *make_expr(enum expr_type type);
71 static expr_t *make_exprl(enum expr_type type, long val);
72 static expr_t *make_exprs(enum expr_type type, char *val);
73 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
74 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
75 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
76 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
77 static type_t *make_type(unsigned char type, type_t *ref);
78 static typeref_t *make_tref(char *name, type_t *ref);
79 static typeref_t *uniq_tref(typeref_t *ref);
80 static type_t *type_ref(typeref_t *ref);
81 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
82 static ifref_t *make_ifref(type_t *iface);
83 static var_t *make_var(char *name);
84 static func_t *make_func(var_t *def, var_t *args);
85 static type_t *make_class(char *name);
86 static type_t *make_safearray(void);
87 static type_t *make_builtin(char *name);
88 static type_t *make_int(int sign);
90 static type_t *reg_type(type_t *type, const char *name, int t);
91 static type_t *reg_typedefs(type_t *type, var_t *names, attr_t *attrs);
92 static type_t *find_type(const char *name, int t);
93 static type_t *find_type2(char *name, int t);
94 static type_t *get_type(unsigned char type, char *name, int t);
95 static type_t *get_typev(unsigned char type, var_t *name, int t);
96 static int get_struct_type(var_t *fields);
98 static var_t *reg_const(var_t *var);
99 static var_t *find_const(char *name, int f);
101 static void write_libid(const char *name, const attr_t *attr);
102 static void write_clsid(type_t *cls);
103 static void write_diid(type_t *iface);
104 static void write_iid(type_t *iface);
106 static int compute_method_indexes(type_t *iface);
107 static char *gen_name(void);
108 static void process_typedefs(var_t *names);
109 static void check_arg(var_t *arg);
111 #define tsENUM 1
112 #define tsSTRUCT 2
113 #define tsUNION 3
116 %union {
117 attr_t *attr;
118 expr_t *expr;
119 type_t *type;
120 typeref_t *tref;
121 var_t *var;
122 func_t *func;
123 ifref_t *ifref;
124 char *str;
125 UUID *uuid;
126 unsigned int num;
129 %token <str> aIDENTIFIER
130 %token <str> aKNOWNTYPE
131 %token <num> aNUM aHEXNUM
132 %token <str> aSTRING
133 %token <uuid> aUUID
134 %token aEOF
135 %token SHL SHR
136 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
137 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
138 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
139 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
140 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
141 %token tDEFAULT
142 %token tDEFAULTCOLLELEM
143 %token tDEFAULTVALUE
144 %token tDEFAULTVTABLE
145 %token tDISPLAYBIND
146 %token tDISPINTERFACE
147 %token tDLLNAME tDOUBLE tDUAL
148 %token tENDPOINT
149 %token tENTRY tENUM tERRORSTATUST
150 %token tEXPLICITHANDLE tEXTERN
151 %token tFALSE
152 %token tFLOAT
153 %token tHANDLE
154 %token tHANDLET
155 %token tHELPCONTEXT tHELPFILE
156 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
157 %token tHIDDEN
158 %token tHYPER tID tIDEMPOTENT
159 %token tIIDIS
160 %token tIMMEDIATEBIND
161 %token tIMPLICITHANDLE
162 %token tIMPORT tIMPORTLIB
163 %token tIN tINLINE
164 %token tINPUTSYNC
165 %token tINT tINT64
166 %token tINTERFACE
167 %token tLCID
168 %token tLENGTHIS tLIBRARY
169 %token tLOCAL
170 %token tLONG
171 %token tMETHODS
172 %token tMODULE
173 %token tNONBROWSABLE
174 %token tNONCREATABLE
175 %token tNONEXTENSIBLE
176 %token tOBJECT tODL tOLEAUTOMATION
177 %token tOPTIONAL
178 %token tOUT
179 %token tPOINTERDEFAULT
180 %token tPROPERTIES
181 %token tPROPGET tPROPPUT tPROPPUTREF
182 %token tPTR
183 %token tPUBLIC
184 %token tRANGE
185 %token tREADONLY tREF
186 %token tREQUESTEDIT
187 %token tRESTRICTED
188 %token tRETVAL
189 %token tSAFEARRAY
190 %token tSHORT
191 %token tSIGNED
192 %token tSINGLE
193 %token tSIZEIS tSIZEOF
194 %token tSMALL
195 %token tSOURCE
196 %token tSTDCALL
197 %token tSTRING tSTRUCT
198 %token tSWITCH tSWITCHIS tSWITCHTYPE
199 %token tTRANSMITAS
200 %token tTRUE
201 %token tTYPEDEF
202 %token tUNION
203 %token tUNIQUE
204 %token tUNSIGNED
205 %token tUUID
206 %token tV1ENUM
207 %token tVARARG
208 %token tVERSION
209 %token tVOID
210 %token tWCHAR tWIREMARSHAL
212 %type <attr> m_attributes attributes attrib_list attribute
213 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
214 %type <expr> array array_list
215 %type <type> inherit interface interfacehdr interfacedef interfacedec
216 %type <type> dispinterface dispinterfacehdr dispinterfacedef
217 %type <type> module modulehdr moduledef
218 %type <type> base_type int_std
219 %type <type> enumdef structdef uniondef
220 %type <ifref> gbl_statements coclass_ints coclass_int
221 %type <tref> type
222 %type <var> m_args no_args args arg
223 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
224 %type <var> m_ident t_ident ident p_ident pident pident_list
225 %type <var> dispint_props
226 %type <func> funcdef int_statements
227 %type <func> dispint_meths
228 %type <type> coclass coclasshdr coclassdef
229 %type <num> pointer_type version
230 %type <str> libraryhdr
232 %left ','
233 %right '?' ':'
234 %left '|'
235 %left '&'
236 %left '-' '+'
237 %left '*' '/'
238 %left SHL SHR
239 %right '~'
240 %right CAST
241 %right PPTR
242 %right NEG
246 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
249 gbl_statements: { $$ = NULL; }
250 | gbl_statements interfacedec { $$ = $1; }
251 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
252 | gbl_statements coclass ';' { $$ = $1;
253 reg_type($2, $2->name, 0);
254 if (!parse_only && do_header) write_coclass_forward($2);
256 | gbl_statements coclassdef { $$ = $1;
257 add_typelib_entry($2);
258 reg_type($2, $2->name, 0);
259 if (!parse_only && do_header) write_coclass_forward($2);
261 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
262 | gbl_statements librarydef { $$ = $1; }
263 | gbl_statements statement { $$ = $1; }
266 imp_statements: {}
267 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
268 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
269 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
270 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
271 reg_type($2, $2->name, 0);
272 if (!parse_only && do_header) write_coclass_forward($2);
274 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
275 | imp_statements statement {}
276 | imp_statements importlib {}
279 int_statements: { $$ = NULL; }
280 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
281 | int_statements statement { $$ = $1; }
284 statement: ';' {}
285 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
286 | cppquote {}
287 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
288 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
289 | import {}
290 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
291 | typedef ';' {}
292 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
295 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
297 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
298 if (!do_import($2)) yychar = aEOF; }
300 import: import_start imp_statements aEOF {}
303 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
306 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
308 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
309 if (!parse_only && do_header) write_library($2, $1);
310 if (!parse_only && do_idfile) write_libid($2, $1);
313 librarydef: library_start imp_statements '}' { end_typelib(); }
316 m_args: { $$ = NULL; }
317 | args
320 no_args: tVOID { $$ = NULL; }
323 args: arg { check_arg($1); $$ = $1; }
324 | args ',' arg { check_arg($3); LINK($3, $1); $$ = $3; }
325 | no_args
328 /* split into two rules to get bison to resolve a tVOID conflict */
329 arg: attributes type pident array { $$ = $3;
330 set_type($$, $2, $4);
331 $$->attrs = $1;
333 | type pident array { $$ = $2;
334 set_type($$, $1, $3);
336 | attributes type pident '(' m_args ')' { $$ = $3;
337 $$->ptr_level--;
338 set_type($$, $2, NULL);
339 $$->attrs = $1;
340 $$->args = $5;
342 | type pident '(' m_args ')' { $$ = $2;
343 $$->ptr_level--;
344 set_type($$, $1, NULL);
345 $$->args = $4;
349 array: { $$ = NULL; }
350 | '[' array_list ']' { $$ = $2; }
351 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
354 array_list: m_expr /* size of first dimension is optional */
355 | array_list ',' expr { LINK($3, $1); $$ = $3; }
356 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
359 m_attributes: { $$ = NULL; }
360 | attributes
363 attributes:
364 '[' attrib_list ']' { $$ = $2;
365 if (!$$)
366 yyerror("empty attribute lists unsupported");
370 attrib_list: attribute
371 | attrib_list ',' attribute { if ($3) { LINK($3, $1); $$ = $3; }
372 else { $$ = $1; }
374 | attrib_list ']' '[' attribute { if ($4) { LINK($4, $1); $$ = $4; }
375 else { $$ = $1; }
379 attribute: { $$ = NULL; }
380 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
381 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
382 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
383 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
384 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
385 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
386 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
387 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
388 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
389 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
390 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
391 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
392 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
393 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
394 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
395 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
396 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
397 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
398 | tDUAL { $$ = make_attr(ATTR_DUAL); }
399 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
400 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
401 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
402 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
403 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
404 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
405 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
406 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
407 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
408 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
409 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
410 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
411 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
412 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
413 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
414 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
415 | tIN { $$ = make_attr(ATTR_IN); }
416 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
417 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
418 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
419 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
420 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
421 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
422 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
423 | tODL { $$ = make_attr(ATTR_ODL); }
424 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
425 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
426 | tOUT { $$ = make_attr(ATTR_OUT); }
427 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
428 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
429 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
430 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
431 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
432 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
433 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
434 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
435 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
436 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
437 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
438 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
439 | tSTRING { $$ = make_attr(ATTR_STRING); }
440 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
441 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
442 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
443 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
444 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
445 | tVARARG { $$ = make_attr(ATTR_VARARG); }
446 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
447 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
448 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
451 callconv:
452 | tSTDCALL
455 cases: { $$ = NULL; }
456 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
457 else { $$ = $1; }
461 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
462 $$ = $4; if (!$$) $$ = make_var(NULL);
463 LINK(a, $$->attrs); $$->attrs = a;
465 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
466 $$ = $3; if (!$$) $$ = make_var(NULL);
467 LINK(a, $$->attrs); $$->attrs = a;
471 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
472 set_type($$, $2, NULL);
473 $$->eval = $5;
477 enums: { $$ = NULL; }
478 | enum_list ',' { $$ = $1; }
479 | enum_list
482 enum_list: enum { if (!$$->eval)
483 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
485 | enum_list ',' enum { LINK($3, $1); $$ = $3;
486 if (!$$->eval)
487 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
491 enum: ident '=' expr_const { $$ = reg_const($1);
492 $$->eval = $3;
493 $$->type = make_int(0);
495 | ident { $$ = reg_const($1);
496 $$->type = make_int(0);
500 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
501 $$->kind = TKIND_ENUM;
502 $$->fields = $4;
503 $$->defined = TRUE;
504 if(in_typelib)
505 add_typelib_entry($$);
509 m_exprs: m_expr
510 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
514 exprs: { $$ = make_expr(EXPR_VOID); }
515 | expr_list
518 expr_list: expr
519 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
523 m_expr: { $$ = make_expr(EXPR_VOID); }
524 | expr
527 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
528 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
529 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
530 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
531 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
532 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
533 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
534 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
535 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
536 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
537 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
538 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
539 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
540 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
541 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
542 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
543 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
544 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
545 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
546 | '(' expr ')' { $$ = $2; }
549 expr_list_const: expr_const
550 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
553 expr_const: expr { $$ = $1;
554 if (!$$->is_const)
555 yyerror("expression is not constant");
559 externdef: tEXTERN tCONST type ident { $$ = $4;
560 set_type($$, $3, NULL);
564 fields: { $$ = NULL; }
565 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
566 else { $$ = $1; }
570 field: s_field ';' { $$ = $1; }
571 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
572 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
573 | ';' { $$ = NULL; }
576 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
579 funcdef:
580 m_attributes type callconv pident
581 '(' m_args ')' { set_type($4, $2, NULL);
582 $4->attrs = $1;
583 $$ = make_func($4, $6);
584 if (is_attr($4->attrs, ATTR_IN)) {
585 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
590 m_ident: { $$ = NULL; }
591 | ident
594 t_ident: { $$ = NULL; }
595 | aIDENTIFIER { $$ = make_var($1); }
596 | aKNOWNTYPE { $$ = make_var($1); }
599 ident: aIDENTIFIER { $$ = make_var($1); }
600 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
601 | aKNOWNTYPE { $$ = make_var($<str>1); }
602 | tASYNC { $$ = make_var($<str>1); }
603 | tID { $$ = make_var($<str>1); }
604 | tLCID { $$ = make_var($<str>1); }
605 | tOBJECT { $$ = make_var($<str>1); }
606 | tRANGE { $$ = make_var($<str>1); }
607 | tRETVAL { $$ = make_var($<str>1); }
608 | tUUID { $$ = make_var($<str>1); }
609 | tVERSION { $$ = make_var($<str>1); }
612 base_type: tBYTE { $$ = make_builtin($<str>1); }
613 | tWCHAR { $$ = make_builtin($<str>1); }
614 | int_std
615 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
616 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
617 switch ($$->type) {
618 case RPC_FC_CHAR: break;
619 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
620 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
621 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
622 case RPC_FC_HYPER:
623 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
625 $$ = alias($$, "MIDL_uhyper");
626 $$->sign = 0;
628 break;
629 default: break;
632 | tUNSIGNED { $$ = make_int(-1); }
633 | tFLOAT { $$ = make_builtin($<str>1); }
634 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
635 | tDOUBLE { $$ = make_builtin($<str>1); }
636 | tBOOLEAN { $$ = make_builtin($<str>1); }
637 | tERRORSTATUST { $$ = make_builtin($<str>1); }
638 | tHANDLET { $$ = make_builtin($<str>1); }
641 m_int:
642 | tINT
645 int_std: tINT { $$ = make_builtin($<str>1); }
646 | tSHORT m_int { $$ = make_builtin($<str>1); }
647 | tSMALL { $$ = make_builtin($<str>1); }
648 | tLONG m_int { $$ = make_builtin($<str>1); }
649 | tHYPER m_int { $$ = make_builtin($<str>1); }
650 | tINT64 { $$ = make_builtin($<str>1); }
651 | tCHAR { $$ = make_builtin($<str>1); }
654 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
655 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
656 if ($$->defined) yyerror("multiple definition error");
657 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
661 coclasshdr: attributes coclass { $$ = $2;
662 $$->attrs = $1;
663 if (!parse_only && do_header)
664 write_coclass($$);
665 if (!parse_only && do_idfile)
666 write_clsid($$);
670 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
671 $$->ifaces = $3;
672 $$->defined = TRUE;
676 coclass_ints: { $$ = NULL; }
677 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
680 coclass_int:
681 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
684 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
685 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
688 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
689 $$ = $2;
690 if ($$->defined) yyerror("multiple definition error");
691 attrs = make_attr(ATTR_DISPINTERFACE);
692 LINK(attrs, $1);
693 $$->attrs = attrs;
694 $$->ref = find_type("IDispatch", 0);
695 if (!$$->ref) yyerror("IDispatch is undefined");
696 $$->defined = TRUE;
697 if (!parse_only && do_header) write_forward($$);
701 dispint_props: tPROPERTIES ':' { $$ = NULL; }
702 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
705 dispint_meths: tMETHODS ':' { $$ = NULL; }
706 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
709 dispinterfacedef: dispinterfacehdr '{'
710 dispint_props
711 dispint_meths
712 '}' { $$ = $1;
713 $$->fields = $3;
714 $$->funcs = $4;
715 if (!parse_only && do_header) write_dispinterface($$);
716 if (!parse_only && do_idfile) write_diid($$);
718 /* FIXME: not sure how to handle this yet
719 | dispinterfacehdr '{' interface '}' { $$ = $1;
720 compute_method_indexes($$);
721 if (!parse_only && do_header) write_interface($$);
722 if (!parse_only && do_idfile) write_iid($$);
727 inherit: { $$ = NULL; }
728 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
731 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
732 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
735 interfacehdr: attributes interface { $$ = $2;
736 if ($$->defined) yyerror("multiple definition error");
737 $$->attrs = $1;
738 $$->defined = TRUE;
739 if (!parse_only && do_header) write_forward($$);
743 interfacedef: interfacehdr inherit
744 '{' int_statements '}' { $$ = $1;
745 $$->ref = $2;
746 $$->funcs = $4;
747 compute_method_indexes($$);
748 if (!parse_only && do_header) write_interface($$);
749 if (!parse_only && do_idfile) write_iid($$);
751 /* MIDL is able to import the definition of a base class from inside the
752 * definition of a derived class, I'll try to support it with this rule */
753 | interfacehdr ':' aIDENTIFIER
754 '{' import int_statements '}' { $$ = $1;
755 $$->ref = find_type2($3, 0);
756 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
757 $$->funcs = $6;
758 compute_method_indexes($$);
759 if (!parse_only && do_header) write_interface($$);
760 if (!parse_only && do_idfile) write_iid($$);
762 | dispinterfacedef { $$ = $1; }
765 interfacedec:
766 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
767 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
770 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
771 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
774 modulehdr: attributes module { $$ = $2;
775 $$->attrs = $1;
779 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
780 $$->funcs = $3;
781 /* FIXME: if (!parse_only && do_header) write_module($$); */
785 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
786 | tCONST p_ident { $$ = $2; /* FIXME */ }
789 pident: ident
790 | p_ident
791 | '(' pident ')' { $$ = $2; }
794 pident_list:
795 pident
796 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
799 pointer_type:
800 tREF { $$ = RPC_FC_RP; }
801 | tUNIQUE { $$ = RPC_FC_UP; }
802 | tPTR { $$ = RPC_FC_FP; }
805 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
806 /* overwrite RPC_FC_STRUCT with a more exact type */
807 $$->type = get_struct_type( $4 );
808 $$->kind = TKIND_RECORD;
809 $$->fields = $4;
810 $$->defined = TRUE;
811 if(in_typelib)
812 add_typelib_entry($$);
816 type: tVOID { $$ = make_tref(NULL, duptype(find_type("void", 0), 1)); }
817 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
818 | base_type { $$ = make_tref(NULL, $1); }
819 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
820 | enumdef { $$ = make_tref(NULL, $1); }
821 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
822 | structdef { $$ = make_tref(NULL, $1); }
823 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
824 | uniondef { $$ = make_tref(NULL, $1); }
825 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
826 | tSAFEARRAY '(' type ')' { $$ = make_tref(NULL, make_safearray()); }
829 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs(type_ref($3), $4, $2);
830 process_typedefs($4);
834 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
835 $$->kind = TKIND_UNION;
836 $$->fields = $4;
837 $$->defined = TRUE;
839 | tUNION t_ident
840 tSWITCH '(' s_field ')'
841 m_ident '{' cases '}' { var_t *u = $7;
842 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
843 $$->kind = TKIND_UNION;
844 if (!u) u = make_var( xstrdup("tagged_union") );
845 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
846 u->type->kind = TKIND_UNION;
847 u->type->fields = $9;
848 u->type->defined = TRUE;
849 LINK(u, $5); $$->fields = u;
850 $$->defined = TRUE;
854 version:
855 aNUM { $$ = MAKELONG($1, 0); }
856 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
861 static void decl_builtin(const char *name, unsigned char type)
863 type_t *t = make_type(type, NULL);
864 t->name = xstrdup(name);
865 reg_type(t, name, 0);
868 static type_t *make_builtin(char *name)
870 /* NAME is strdup'd in the lexer */
871 type_t *t = duptype(find_type(name, 0), 0);
872 t->name = name;
873 return t;
876 static type_t *make_int(int sign)
878 type_t *t = duptype(find_type("int", 0), 1);
880 t->sign = sign;
881 if (sign < 0)
882 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
884 return t;
887 void init_types(void)
889 decl_builtin("void", 0);
890 decl_builtin("byte", RPC_FC_BYTE);
891 decl_builtin("wchar_t", RPC_FC_WCHAR);
892 decl_builtin("int", RPC_FC_LONG); /* win32 */
893 decl_builtin("short", RPC_FC_SHORT);
894 decl_builtin("small", RPC_FC_SMALL);
895 decl_builtin("long", RPC_FC_LONG);
896 decl_builtin("hyper", RPC_FC_HYPER);
897 decl_builtin("__int64", RPC_FC_HYPER);
898 decl_builtin("char", RPC_FC_CHAR);
899 decl_builtin("float", RPC_FC_FLOAT);
900 decl_builtin("double", RPC_FC_DOUBLE);
901 decl_builtin("boolean", RPC_FC_BYTE);
902 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
903 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
906 static attr_t *make_attr(enum attr_type type)
908 attr_t *a = xmalloc(sizeof(attr_t));
909 a->type = type;
910 a->u.ival = 0;
911 INIT_LINK(a);
912 return a;
915 static attr_t *make_attrv(enum attr_type type, unsigned long val)
917 attr_t *a = xmalloc(sizeof(attr_t));
918 a->type = type;
919 a->u.ival = val;
920 INIT_LINK(a);
921 return a;
924 static attr_t *make_attrp(enum attr_type type, void *val)
926 attr_t *a = xmalloc(sizeof(attr_t));
927 a->type = type;
928 a->u.pval = val;
929 INIT_LINK(a);
930 return a;
933 static expr_t *make_expr(enum expr_type type)
935 expr_t *e = xmalloc(sizeof(expr_t));
936 e->type = type;
937 e->ref = NULL;
938 e->u.lval = 0;
939 e->is_const = FALSE;
940 INIT_LINK(e);
941 return e;
944 static expr_t *make_exprl(enum expr_type type, long val)
946 expr_t *e = xmalloc(sizeof(expr_t));
947 e->type = type;
948 e->ref = NULL;
949 e->u.lval = val;
950 e->is_const = FALSE;
951 INIT_LINK(e);
952 /* check for numeric constant */
953 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
954 /* make sure true/false value is valid */
955 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
956 e->is_const = TRUE;
957 e->cval = val;
959 return e;
962 static expr_t *make_exprs(enum expr_type type, char *val)
964 expr_t *e;
965 e = xmalloc(sizeof(expr_t));
966 e->type = type;
967 e->ref = NULL;
968 e->u.sval = val;
969 e->is_const = FALSE;
970 INIT_LINK(e);
971 /* check for predefined constants */
972 if (type == EXPR_IDENTIFIER) {
973 var_t *c = find_const(val, 0);
974 if (c) {
975 e->u.sval = c->name;
976 free(val);
977 e->is_const = TRUE;
978 e->cval = c->eval->cval;
981 return e;
984 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
986 expr_t *e;
987 e = xmalloc(sizeof(expr_t));
988 e->type = type;
989 e->ref = expr;
990 e->u.tref = tref;
991 e->is_const = FALSE;
992 INIT_LINK(e);
993 /* check for cast of constant expression */
994 if (type == EXPR_SIZEOF) {
995 switch (tref->ref->type) {
996 case RPC_FC_BYTE:
997 case RPC_FC_CHAR:
998 case RPC_FC_SMALL:
999 case RPC_FC_USMALL:
1000 e->is_const = TRUE;
1001 e->cval = 1;
1002 break;
1003 case RPC_FC_WCHAR:
1004 case RPC_FC_USHORT:
1005 case RPC_FC_SHORT:
1006 e->is_const = TRUE;
1007 e->cval = 2;
1008 break;
1009 case RPC_FC_LONG:
1010 case RPC_FC_ULONG:
1011 case RPC_FC_FLOAT:
1012 case RPC_FC_ERROR_STATUS_T:
1013 e->is_const = TRUE;
1014 e->cval = 4;
1015 break;
1016 case RPC_FC_HYPER:
1017 case RPC_FC_DOUBLE:
1018 e->is_const = TRUE;
1019 e->cval = 8;
1020 break;
1023 if (type == EXPR_CAST && expr->is_const) {
1024 e->is_const = TRUE;
1025 e->cval = expr->cval;
1027 return e;
1030 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1032 expr_t *e;
1033 e = xmalloc(sizeof(expr_t));
1034 e->type = type;
1035 e->ref = expr;
1036 e->u.lval = 0;
1037 e->is_const = FALSE;
1038 INIT_LINK(e);
1039 /* check for compile-time optimization */
1040 if (expr->is_const) {
1041 e->is_const = TRUE;
1042 switch (type) {
1043 case EXPR_NEG:
1044 e->cval = -expr->cval;
1045 break;
1046 case EXPR_NOT:
1047 e->cval = ~expr->cval;
1048 break;
1049 default:
1050 e->is_const = FALSE;
1051 break;
1054 return e;
1057 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1059 expr_t *e;
1060 e = xmalloc(sizeof(expr_t));
1061 e->type = type;
1062 e->ref = expr1;
1063 e->u.ext = expr2;
1064 e->is_const = FALSE;
1065 INIT_LINK(e);
1066 /* check for compile-time optimization */
1067 if (expr1->is_const && expr2->is_const) {
1068 e->is_const = TRUE;
1069 switch (type) {
1070 case EXPR_ADD:
1071 e->cval = expr1->cval + expr2->cval;
1072 break;
1073 case EXPR_SUB:
1074 e->cval = expr1->cval - expr2->cval;
1075 break;
1076 case EXPR_MUL:
1077 e->cval = expr1->cval * expr2->cval;
1078 break;
1079 case EXPR_DIV:
1080 e->cval = expr1->cval / expr2->cval;
1081 break;
1082 case EXPR_OR:
1083 e->cval = expr1->cval | expr2->cval;
1084 break;
1085 case EXPR_AND:
1086 e->cval = expr1->cval & expr2->cval;
1087 break;
1088 case EXPR_SHL:
1089 e->cval = expr1->cval << expr2->cval;
1090 break;
1091 case EXPR_SHR:
1092 e->cval = expr1->cval >> expr2->cval;
1093 break;
1094 default:
1095 e->is_const = FALSE;
1096 break;
1099 return e;
1102 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1104 expr_t *e;
1105 e = xmalloc(sizeof(expr_t));
1106 e->type = type;
1107 e->ref = expr1;
1108 e->u.ext = expr2;
1109 e->ext2 = expr3;
1110 e->is_const = FALSE;
1111 INIT_LINK(e);
1112 /* check for compile-time optimization */
1113 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1114 e->is_const = TRUE;
1115 switch (type) {
1116 case EXPR_COND:
1117 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1118 break;
1119 default:
1120 e->is_const = FALSE;
1121 break;
1124 return e;
1127 static type_t *make_type(unsigned char type, type_t *ref)
1129 type_t *t = xmalloc(sizeof(type_t));
1130 t->name = NULL;
1131 t->kind = TKIND_PRIMITIVE;
1132 t->type = type;
1133 t->ref = ref;
1134 t->attrs = NULL;
1135 t->orig = NULL;
1136 t->funcs = NULL;
1137 t->fields = NULL;
1138 t->ifaces = NULL;
1139 t->ignore = parse_only;
1140 t->is_const = FALSE;
1141 t->sign = 0;
1142 t->defined = FALSE;
1143 t->written = FALSE;
1144 t->user_types_registered = FALSE;
1145 t->typelib_idx = -1;
1146 INIT_LINK(t);
1147 return t;
1150 static typeref_t *make_tref(char *name, type_t *ref)
1152 typeref_t *t = xmalloc(sizeof(typeref_t));
1153 t->name = name;
1154 t->ref = ref;
1155 t->uniq = ref ? 0 : 1;
1156 return t;
1159 static typeref_t *uniq_tref(typeref_t *ref)
1161 typeref_t *t = ref;
1162 type_t *tp;
1163 if (t->uniq) return t;
1165 if (t->name)
1167 tp = duptype(t->ref, 0);
1168 tp->name = t->name;
1170 else
1171 tp = duptype(t->ref, 1);
1173 t->name = NULL;
1174 t->ref = tp;
1175 t->uniq = 1;
1176 return t;
1179 static type_t *type_ref(typeref_t *ref)
1181 type_t *t = ref->ref;
1182 free(ref->name);
1183 free(ref);
1184 return t;
1187 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1189 v->type = ref->ref;
1190 v->tname = ref->name;
1191 ref->name = NULL;
1192 free(ref);
1193 v->array = arr;
1196 static ifref_t *make_ifref(type_t *iface)
1198 ifref_t *l = xmalloc(sizeof(ifref_t));
1199 l->iface = iface;
1200 l->attrs = NULL;
1201 INIT_LINK(l);
1202 return l;
1205 static var_t *make_var(char *name)
1207 var_t *v = xmalloc(sizeof(var_t));
1208 v->name = name;
1209 v->ptr_level = 0;
1210 v->type = NULL;
1211 v->args = NULL;
1212 v->tname = NULL;
1213 v->attrs = NULL;
1214 v->array = NULL;
1215 v->eval = NULL;
1216 INIT_LINK(v);
1217 return v;
1220 static func_t *make_func(var_t *def, var_t *args)
1222 func_t *f = xmalloc(sizeof(func_t));
1223 f->def = def;
1224 f->args = args;
1225 f->ignore = parse_only;
1226 f->idx = -1;
1227 INIT_LINK(f);
1228 return f;
1231 static type_t *make_class(char *name)
1233 type_t *c = make_type(0, NULL);
1234 c->name = name;
1235 c->kind = TKIND_COCLASS;
1236 INIT_LINK(c);
1237 return c;
1240 static type_t *make_safearray(void)
1242 return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
1245 #define HASHMAX 64
1247 static int hash_ident(const char *name)
1249 const char *p = name;
1250 int sum = 0;
1251 /* a simple sum hash is probably good enough */
1252 while (*p) {
1253 sum += *p;
1254 p++;
1256 return sum & (HASHMAX-1);
1259 /***** type repository *****/
1261 struct rtype {
1262 const char *name;
1263 type_t *type;
1264 int t;
1265 struct rtype *next;
1268 struct rtype *type_hash[HASHMAX];
1270 static type_t *reg_type(type_t *type, const char *name, int t)
1272 struct rtype *nt;
1273 int hash;
1274 if (!name) {
1275 yyerror("registering named type without name");
1276 return type;
1278 hash = hash_ident(name);
1279 nt = xmalloc(sizeof(struct rtype));
1280 nt->name = name;
1281 nt->type = type;
1282 nt->t = t;
1283 nt->next = type_hash[hash];
1284 type_hash[hash] = nt;
1285 return type;
1288 static type_t *reg_typedefs(type_t *type, var_t *names, attr_t *attrs)
1290 type_t *ptr = type;
1291 int ptrc = 0;
1292 int is_str = is_attr(attrs, ATTR_STRING);
1293 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1295 if (is_str)
1297 type_t *t = type;
1298 unsigned char c;
1300 while (is_ptr(t))
1301 t = t->ref;
1303 c = t->type;
1304 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1305 yyerror("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays",
1306 names->name);
1309 /* We must generate names for tagless enum, struct or union.
1310 Typedef-ing a tagless enum, struct or union means we want the typedef
1311 to be included in a library whether it has other attributes or not,
1312 hence the public attribute. */
1313 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1314 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1316 if (! is_attr(attrs, ATTR_PUBLIC))
1318 attr_t *new_attrs = make_attr(ATTR_PUBLIC);
1319 LINK(new_attrs, attrs);
1320 attrs = new_attrs;
1322 type->name = gen_name();
1325 while (names) {
1326 var_t *next = NEXT_LINK(names);
1327 if (names->name) {
1328 type_t *cur = ptr;
1329 int cptr = names->ptr_level;
1330 if (cptr > ptrc) {
1331 while (cptr > ptrc) {
1332 cur = ptr = make_type(RPC_FC_RP, cur);
1333 ptrc++;
1335 } else {
1336 while (cptr < ptrc) {
1337 cur = cur->ref;
1338 cptr++;
1341 cur = alias(cur, names->name);
1342 cur->attrs = attrs;
1343 if (ptr_type)
1345 if (is_ptr(cur))
1346 cur->type = ptr_type;
1347 else
1348 yyerror("'%s': pointer attribute applied to non-pointer type",
1349 cur->name);
1351 else if (is_str && ! is_ptr(cur))
1352 yyerror("'%s': [string] attribute applied to non-pointer type",
1353 cur->name);
1355 reg_type(cur, cur->name, 0);
1357 names = next;
1359 return type;
1362 static type_t *find_type(const char *name, int t)
1364 struct rtype *cur = type_hash[hash_ident(name)];
1365 while (cur && (cur->t != t || strcmp(cur->name, name)))
1366 cur = cur->next;
1367 if (!cur) {
1368 yyerror("type '%s' not found", name);
1369 return NULL;
1371 return cur->type;
1374 static type_t *find_type2(char *name, int t)
1376 type_t *tp = find_type(name, t);
1377 free(name);
1378 return tp;
1381 int is_type(const char *name)
1383 struct rtype *cur = type_hash[hash_ident(name)];
1384 while (cur && (cur->t || strcmp(cur->name, name)))
1385 cur = cur->next;
1386 if (cur) return TRUE;
1387 return FALSE;
1390 static type_t *get_type(unsigned char type, char *name, int t)
1392 struct rtype *cur = NULL;
1393 type_t *tp;
1394 if (name) {
1395 cur = type_hash[hash_ident(name)];
1396 while (cur && (cur->t != t || strcmp(cur->name, name)))
1397 cur = cur->next;
1399 if (cur) {
1400 free(name);
1401 return cur->type;
1403 tp = make_type(type, NULL);
1404 tp->name = name;
1405 if (!name) return tp;
1406 return reg_type(tp, name, t);
1409 static type_t *get_typev(unsigned char type, var_t *name, int t)
1411 char *sname = NULL;
1412 if (name) {
1413 sname = name->name;
1414 free(name);
1416 return get_type(type, sname, t);
1419 static int get_struct_type(var_t *field)
1421 int has_pointer = 0;
1422 int has_conformance = 0;
1423 int has_variance = 0;
1425 for (; field; field = NEXT_LINK(field))
1427 type_t *t = field->type;
1429 if (field->ptr_level > 0)
1431 has_pointer = 1;
1432 continue;
1435 if (is_string_type(field->attrs, 0, field->array))
1437 has_conformance = 1;
1438 has_variance = 1;
1439 continue;
1442 if (is_array_type(field->attrs, 0, field->array))
1444 if (field->array && !field->array->is_const)
1446 has_conformance = 1;
1447 if (PREV_LINK(field))
1448 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1449 field->name);
1451 if (is_attr(field->attrs, ATTR_LENGTHIS))
1452 has_variance = 1;
1455 switch (t->type)
1458 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1459 * Simple types don't effect the type of struct.
1460 * A struct containing a simple struct is still a simple struct.
1461 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1463 case 0: /* void pointer */
1464 case RPC_FC_BYTE:
1465 case RPC_FC_CHAR:
1466 case RPC_FC_SMALL:
1467 case RPC_FC_USMALL:
1468 case RPC_FC_WCHAR:
1469 case RPC_FC_SHORT:
1470 case RPC_FC_USHORT:
1471 case RPC_FC_LONG:
1472 case RPC_FC_ULONG:
1473 case RPC_FC_INT3264:
1474 case RPC_FC_UINT3264:
1475 case RPC_FC_HYPER:
1476 case RPC_FC_FLOAT:
1477 case RPC_FC_DOUBLE:
1478 case RPC_FC_STRUCT:
1479 case RPC_FC_ENUM16:
1480 case RPC_FC_ENUM32:
1481 break;
1483 case RPC_FC_RP:
1484 case RPC_FC_UP:
1485 case RPC_FC_FP:
1486 case RPC_FC_OP:
1487 has_pointer = 1;
1488 break;
1489 case RPC_FC_CARRAY:
1490 has_conformance = 1;
1491 if (PREV_LINK(field))
1492 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1493 field->name);
1494 break;
1497 * Propagate member attributes
1498 * a struct should be at least as complex as its member
1500 case RPC_FC_CVSTRUCT:
1501 has_conformance = 1;
1502 has_variance = 1;
1503 has_pointer = 1;
1504 break;
1506 case RPC_FC_CPSTRUCT:
1507 has_conformance = 1;
1508 if (PREV_LINK(field))
1509 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1510 field->name);
1511 has_pointer = 1;
1512 break;
1514 case RPC_FC_CSTRUCT:
1515 has_conformance = 1;
1516 if (PREV_LINK(field))
1517 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1518 field->name);
1519 break;
1521 case RPC_FC_PSTRUCT:
1522 has_pointer = 1;
1523 break;
1525 default:
1526 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1527 field->name, t->type);
1528 /* fallthru - treat it as complex */
1530 /* as soon as we see one of these these members, it's bogus... */
1531 case RPC_FC_IP:
1532 case RPC_FC_ENCAPSULATED_UNION:
1533 case RPC_FC_NON_ENCAPSULATED_UNION:
1534 case RPC_FC_TRANSMIT_AS:
1535 case RPC_FC_REPRESENT_AS:
1536 case RPC_FC_PAD:
1537 case RPC_FC_EMBEDDED_COMPLEX:
1538 case RPC_FC_BOGUS_STRUCT:
1539 return RPC_FC_BOGUS_STRUCT;
1543 if( has_variance )
1544 return RPC_FC_CVSTRUCT;
1545 if( has_conformance && has_pointer )
1546 return RPC_FC_CPSTRUCT;
1547 if( has_conformance )
1548 return RPC_FC_CSTRUCT;
1549 if( has_pointer )
1550 return RPC_FC_PSTRUCT;
1551 return RPC_FC_STRUCT;
1554 /***** constant repository *****/
1556 struct rconst {
1557 char *name;
1558 var_t *var;
1559 struct rconst *next;
1562 struct rconst *const_hash[HASHMAX];
1564 static var_t *reg_const(var_t *var)
1566 struct rconst *nc;
1567 int hash;
1568 if (!var->name) {
1569 yyerror("registering constant without name");
1570 return var;
1572 hash = hash_ident(var->name);
1573 nc = xmalloc(sizeof(struct rconst));
1574 nc->name = var->name;
1575 nc->var = var;
1576 nc->next = const_hash[hash];
1577 const_hash[hash] = nc;
1578 return var;
1581 static var_t *find_const(char *name, int f)
1583 struct rconst *cur = const_hash[hash_ident(name)];
1584 while (cur && strcmp(cur->name, name))
1585 cur = cur->next;
1586 if (!cur) {
1587 if (f) yyerror("constant '%s' not found", name);
1588 return NULL;
1590 return cur->var;
1593 static void write_libid(const char *name, const attr_t *attr)
1595 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1596 write_guid(idfile, "LIBID", name, uuid);
1599 static void write_clsid(type_t *cls)
1601 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1602 write_guid(idfile, "CLSID", cls->name, uuid);
1605 static void write_diid(type_t *iface)
1607 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1608 write_guid(idfile, "DIID", iface->name, uuid);
1611 static void write_iid(type_t *iface)
1613 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1614 write_guid(idfile, "IID", iface->name, uuid);
1617 static int compute_method_indexes(type_t *iface)
1619 int idx;
1620 func_t *f = iface->funcs;
1622 if (iface->ref)
1623 idx = compute_method_indexes(iface->ref);
1624 else
1625 idx = 0;
1627 if (! f)
1628 return idx;
1630 while (NEXT_LINK(f))
1631 f = NEXT_LINK(f);
1633 for ( ; f ; f = PREV_LINK(f))
1634 if (! is_callas(f->def->attrs))
1635 f->idx = idx++;
1637 return idx;
1640 static char *gen_name(void)
1642 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1643 static unsigned long n = 0;
1644 static const char *file_id;
1645 static size_t size;
1646 char *name;
1648 if (! file_id)
1650 char *dst = dup_basename(input_name, ".idl");
1651 file_id = dst;
1653 for (; *dst; ++dst)
1654 if (! isalnum((unsigned char) *dst))
1655 *dst = '_';
1657 size = sizeof format - 7 + strlen(file_id) + 8;
1660 name = xmalloc(size);
1661 sprintf(name, format, file_id, n++);
1662 return name;
1665 static void process_typedefs(var_t *names)
1667 END_OF_LIST(names);
1668 while (names)
1670 var_t *next = PREV_LINK(names);
1671 type_t *type = find_type(names->name, 0);
1673 if (! parse_only && do_header)
1674 write_typedef(type);
1675 if (in_typelib && type->attrs)
1676 add_typelib_entry(type);
1678 free(names);
1679 names = next;
1683 static void check_arg(var_t *arg)
1685 type_t *t = arg->type;
1687 if (t->type == 0 && ! is_var_ptr(arg))
1688 yyerror("argument '%s' has void type", arg->name);