Release 20040914.
[wine/gsoc-2012-control.git] / tools / widl / parser.y
blob001c6f4218c9d450897d233a36e35c11c021371f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <string.h>
30 #ifdef HAVE_ALLOCA_H
31 #include <alloca.h>
32 #endif
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "proxy.h"
39 #include "typelib.h"
41 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
63 # endif
64 #endif
66 static attr_t *make_attr(enum attr_type type);
67 static attr_t *make_attrv(enum attr_type type, unsigned long val);
68 static attr_t *make_attrp(enum attr_type type, void *val);
69 static expr_t *make_expr(enum expr_type type);
70 static expr_t *make_exprl(enum expr_type type, long val);
71 static expr_t *make_exprs(enum expr_type type, char *val);
72 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
73 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
74 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
75 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
76 static type_t *make_type(unsigned char type, type_t *ref);
77 static typeref_t *make_tref(char *name, type_t *ref);
78 static typeref_t *uniq_tref(typeref_t *ref);
79 static type_t *type_ref(typeref_t *ref);
80 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
81 static ifref_t *make_ifref(type_t *iface);
82 static var_t *make_var(char *name);
83 static func_t *make_func(var_t *def, var_t *args);
84 static class_t *make_class(char *name);
86 static type_t *reg_type(type_t *type, char *name, int t);
87 static type_t *reg_types(type_t *type, var_t *names, int t);
88 static type_t *find_type(char *name, int t);
89 static type_t *find_type2(char *name, int t);
90 static type_t *get_type(unsigned char type, char *name, int t);
91 static type_t *get_typev(unsigned char type, var_t *name, int t);
92 static int get_struct_type(var_t *fields);
94 static var_t *reg_const(var_t *var);
95 static var_t *find_const(char *name, int f);
97 #define tsENUM 1
98 #define tsSTRUCT 2
99 #define tsUNION 3
101 static type_t std_bool = { "boolean" };
102 static type_t std_int = { "int" };
103 static type_t std_int64 = { "__int64" };
104 static type_t std_uhyper = { "MIDL_uhyper" };
107 %union {
108 attr_t *attr;
109 expr_t *expr;
110 type_t *type;
111 typeref_t *tref;
112 var_t *var;
113 func_t *func;
114 ifref_t *ifref;
115 class_t *clas;
116 char *str;
117 UUID *uuid;
118 unsigned int num;
121 %token <str> aIDENTIFIER
122 %token <str> aKNOWNTYPE
123 %token <num> aNUM aHEXNUM
124 %token <str> aSTRING
125 %token <uuid> aUUID
126 %token aEOF
127 %token SHL SHR
128 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
129 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
130 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
131 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
132 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
133 %token tDEFAULT
134 %token tDISPINTERFACE
135 %token tDLLNAME tDOUBLE tDUAL
136 %token tENDPOINT
137 %token tENTRY tENUM tERRORSTATUST
138 %token tEXTERN
139 %token tFLOAT
140 %token tHANDLE
141 %token tHANDLET
142 %token tHELPSTRING
143 %token tHYPER tID tIDEMPOTENT
144 %token tIIDIS
145 %token tIMPORT tIMPORTLIB
146 %token tIN tINCLUDE tINLINE
147 %token tINPUTSYNC
148 %token tINT tINT64
149 %token tINTERFACE
150 %token tLENGTHIS tLIBRARY
151 %token tLOCAL
152 %token tLONG
153 %token tMETHODS
154 %token tMODULE
155 %token tOBJECT tODL tOLEAUTOMATION
156 %token tOUT
157 %token tPOINTERDEFAULT
158 %token tPROPERTIES
159 %token tPUBLIC
160 %token tREADONLY tREF
161 %token tRETVAL
162 %token tSHORT
163 %token tSIGNED
164 %token tSIZEIS tSIZEOF
165 %token tSOURCE
166 %token tSTDCALL
167 %token tSTRING tSTRUCT
168 %token tSWITCH tSWITCHIS tSWITCHTYPE
169 %token tTRANSMITAS
170 %token tTYPEDEF
171 %token tUNION
172 %token tUNIQUE
173 %token tUNSIGNED
174 %token tUUID
175 %token tV1ENUM
176 %token tVERSION
177 %token tVOID
178 %token tWCHAR tWIREMARSHAL
180 /* used in attr_t */
181 %token tPOINTERTYPE
183 %type <attr> m_attributes attributes attrib_list attribute
184 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
185 %type <expr> array array_list
186 %type <type> inherit interface interfacehdr interfacedef interfacedec
187 %type <type> dispinterface dispinterfacehdr dispinterfacedef
188 %type <type> module modulehdr moduledef
189 %type <type> base_type int_std
190 %type <type> enumdef structdef typedef uniondef
191 %type <ifref> gbl_statements coclass_ints coclass_int
192 %type <tref> type
193 %type <var> m_args no_args args arg
194 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
195 %type <var> m_ident t_ident ident p_ident pident pident_list
196 %type <var> dispint_props
197 %type <func> funcdef int_statements
198 %type <func> dispint_meths
199 %type <clas> coclass coclasshdr coclassdef
200 %type <num> pointer_type version
201 %type <str> libraryhdr
203 %left ','
204 %left '|'
205 %left '&'
206 %left '-' '+'
207 %left '*' '/'
208 %left SHL SHR
209 %right '~'
210 %right CAST
211 %right PPTR
212 %right NEG
216 input: gbl_statements { write_proxies($1); }
219 gbl_statements: { $$ = NULL; }
220 | gbl_statements interfacedec { $$ = $1; }
221 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
222 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
223 | gbl_statements moduledef { $$ = $1; add_module($2); }
224 | gbl_statements librarydef { $$ = $1; }
225 | gbl_statements statement { $$ = $1; }
228 imp_statements: {}
229 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
230 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
231 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
232 | imp_statements moduledef { if (!parse_only) add_module($2); }
233 | imp_statements statement {}
236 int_statements: { $$ = NULL; }
237 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
238 | int_statements statement { $$ = $1; }
241 statement: ';' {}
242 | constdef ';' { if (!parse_only) { write_constdef($1); } }
243 | cppquote {}
244 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
245 | externdef ';' { if (!parse_only) { write_externdef($1); } }
246 | import {}
247 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
248 | typedef ';' {}
249 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
252 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
254 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
255 if (!do_import($2)) yychar = aEOF; }
257 import: import_start imp_statements aEOF {}
260 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
262 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
264 librarydef: library_start imp_statements '}' { end_typelib(); }
267 m_args: { $$ = NULL; }
268 | args
271 no_args: tVOID { $$ = NULL; }
274 args: arg
275 | args ',' arg { LINK($3, $1); $$ = $3; }
276 | no_args
279 /* split into two rules to get bison to resolve a tVOID conflict */
280 arg: attributes type pident array { $$ = $3;
281 set_type($$, $2, $4);
282 $$->attrs = $1;
284 | type pident array { $$ = $2;
285 set_type($$, $1, $3);
287 | attributes type pident '(' m_args ')' { $$ = $3;
288 $$->ptr_level--;
289 set_type($$, $2, NULL);
290 $$->attrs = $1;
291 $$->args = $5;
293 | type pident '(' m_args ')' { $$ = $2;
294 $$->ptr_level--;
295 set_type($$, $1, NULL);
296 $$->args = $4;
300 array: { $$ = NULL; }
301 | '[' array_list ']' { $$ = $2; }
302 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
305 array_list: m_expr /* size of first dimension is optional */
306 | array_list ',' expr { LINK($3, $1); $$ = $3; }
307 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
310 m_attributes: { $$ = NULL; }
311 | attributes
314 attributes:
315 '[' attrib_list ']' { $$ = $2; }
318 attrib_list: attribute
319 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
320 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
323 attribute:
324 tASYNC { $$ = make_attr(ATTR_ASYNC); }
325 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
326 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
327 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
328 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
329 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
330 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
331 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
332 | tDUAL { $$ = make_attr(ATTR_DUAL); }
333 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
334 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
335 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
336 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
337 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
338 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
339 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
340 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
341 | tIN { $$ = make_attr(ATTR_IN); }
342 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
343 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
344 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
345 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
346 | tODL { $$ = make_attr(ATTR_ODL); }
347 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
348 | tOUT { $$ = make_attr(ATTR_OUT); }
349 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
350 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
351 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
352 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
353 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
354 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
355 | tSTRING { $$ = make_attr(ATTR_STRING); }
356 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
357 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
358 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
359 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
360 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
361 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
362 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
363 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
366 callconv:
367 | tSTDCALL
370 cases: { $$ = NULL; }
371 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
372 else { $$ = $1; }
376 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
377 $$ = $4; if (!$$) $$ = make_var(NULL);
378 LINK(a, $$->attrs); $$->attrs = a;
380 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
381 $$ = $3; if (!$$) $$ = make_var(NULL);
382 LINK(a, $$->attrs); $$->attrs = a;
386 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
387 set_type($$, $2, NULL);
388 $$->eval = $5;
389 $$->lval = $5->cval;
393 enums: { $$ = NULL; }
394 | enum_list ',' { $$ = $1; }
395 | enum_list
398 enum_list: enum
399 | enum_list ',' enum { LINK($3, $1); $$ = $3;
400 if ($1 && !$3->eval)
401 $3->lval = $1->lval + 1;
405 enum: ident '=' expr_const { $$ = reg_const($1);
406 $$->eval = $3;
407 $$->lval = $3->cval;
409 | ident { $$ = reg_const($1);
410 $$->lval = 0; /* default for first enum entry */
414 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
415 $$->fields = $4;
416 $$->defined = TRUE;
420 m_exprs: m_expr
421 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
425 exprs: { $$ = make_expr(EXPR_VOID); }
426 | expr_list
429 expr_list: expr
430 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
434 m_expr: { $$ = make_expr(EXPR_VOID); }
435 | expr
438 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
439 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
440 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
441 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
442 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
443 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
444 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
445 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
446 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
447 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
448 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
449 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
450 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
451 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
452 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
453 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
454 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
455 | '(' expr ')' { $$ = $2; }
458 expr_list_const: expr_const
459 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
462 expr_const: expr { $$ = $1;
463 if (!$$->is_const) yyerror("expression is not constant\n");
467 externdef: tEXTERN tCONST type ident { $$ = $4;
468 set_type($$, $3, NULL);
472 fields: { $$ = NULL; }
473 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
474 else { $$ = $1; }
478 field: s_field ';' { $$ = $1; }
479 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
480 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
481 | ';' { $$ = NULL; }
484 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
487 funcdef:
488 m_attributes type callconv pident
489 '(' m_args ')' { set_type($4, $2, NULL);
490 $4->attrs = $1;
491 $$ = make_func($4, $6);
495 m_ident: { $$ = NULL; }
496 | ident
499 t_ident: { $$ = NULL; }
500 | aIDENTIFIER { $$ = make_var($1); }
501 | aKNOWNTYPE { $$ = make_var($1); }
504 ident: aIDENTIFIER { $$ = make_var($1); }
505 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
506 | tID { $$ = make_var($<str>1); }
507 | tRETVAL { $$ = make_var($<str>1); }
508 | tVERSION { $$ = make_var($<str>1); }
511 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
512 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
513 | int_std
514 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
515 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
516 switch ($$->type) {
517 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
518 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
519 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
520 case RPC_FC_HYPER:
521 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
522 break;
523 default: break;
526 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
527 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
528 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
529 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
530 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
533 m_int:
534 | tINT
537 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
538 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
539 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
540 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
541 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
542 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
545 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
546 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
549 coclasshdr: attributes coclass { $$ = $2;
550 $$->attrs = $1;
554 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
555 $$->ifaces = $3;
559 coclass_ints: { $$ = NULL; }
560 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
563 coclass_int:
564 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
567 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
568 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
571 dispinterfacehdr: attributes dispinterface { $$ = $2;
572 if ($$->defined) yyerror("multiple definition error\n");
573 $$->attrs = $1;
574 /* $$->attrs = make_attr(ATTR_DISPINTERFACE); */
575 /* LINK($$->attrs, $1); */
576 $$->ref = find_type("IDispatch", 0);
577 if (!$$->ref) yyerror("IDispatch is undefined\n");
578 $$->defined = TRUE;
579 if (!parse_only) write_forward($$);
583 dispint_props: tPROPERTIES ':' { $$ = NULL; }
584 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
587 dispint_meths: tMETHODS ':' { $$ = NULL; }
588 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
591 dispinterfacedef: dispinterfacehdr '{'
592 dispint_props
593 dispint_meths
594 '}' { $$ = $1;
595 $$->fields = $3;
596 $$->funcs = $4;
597 if (!parse_only) write_interface($$);
599 /* FIXME: not sure how to handle this yet
600 | dispinterfacehdr '{' interface '}' { $$ = $1;
601 if (!parse_only) write_interface($$);
606 inherit: { $$ = NULL; }
607 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
610 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
611 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
614 interfacehdr: attributes interface { $$ = $2;
615 if ($$->defined) yyerror("multiple definition error\n");
616 $$->attrs = $1;
617 $$->defined = TRUE;
618 if (!parse_only) write_forward($$);
622 interfacedef: interfacehdr inherit
623 '{' int_statements '}' { $$ = $1;
624 $$->ref = $2;
625 $$->funcs = $4;
626 if (!parse_only) write_interface($$);
628 /* MIDL is able to import the definition of a base class from inside the
629 * definition of a derived class, I'll try to support it with this rule */
630 | interfacehdr ':' aIDENTIFIER
631 '{' import int_statements '}' { $$ = $1;
632 $$->ref = find_type2($3, 0);
633 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
634 $$->funcs = $6;
635 if (!parse_only) write_interface($$);
637 | dispinterfacedef { $$ = $1; }
640 interfacedec:
641 interface ';' { $$ = $1; if (!parse_only) write_forward($$); }
642 | dispinterface ';' { $$ = $1; if (!parse_only) write_forward($$); }
645 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
646 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
649 modulehdr: attributes module { $$ = $2;
650 $$->attrs = $1;
654 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
655 $$->funcs = $3;
656 /* FIXME: if (!parse_only) write_module($$); */
660 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
661 | tCONST p_ident { $$ = $2; /* FIXME */ }
664 pident: ident
665 | p_ident
666 | '(' pident ')' { $$ = $2; }
669 pident_list:
670 pident
671 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
674 pointer_type:
675 tREF { $$ = RPC_FC_RP; }
676 | tUNIQUE { $$ = RPC_FC_UP; }
679 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
680 /* overwrite RPC_FC_STRUCT with a more exact type */
681 $$->type = get_struct_type( $4 );
682 $$->fields = $4;
683 $$->defined = TRUE;
687 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
688 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
689 | base_type { $$ = make_tref(NULL, $1); }
690 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
691 | enumdef { $$ = make_tref(NULL, $1); }
692 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
693 | structdef { $$ = make_tref(NULL, $1); }
694 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
695 | uniondef { $$ = make_tref(NULL, $1); }
696 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
699 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
700 $4->tname = tref->name;
701 tref->name = NULL;
702 $$ = type_ref(tref);
703 $$->attrs = $2;
704 if (!parse_only) write_typedef($$, $4);
705 reg_types($$, $4, 0);
709 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
710 $$->fields = $4;
711 $$->defined = TRUE;
713 | tUNION t_ident
714 tSWITCH '(' s_field ')'
715 m_ident '{' cases '}' { var_t *u = $7;
716 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
717 if (!u) u = make_var("tagged_union");
718 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
719 u->type->fields = $9;
720 u->type->defined = TRUE;
721 LINK(u, $5); $$->fields = u;
722 $$->defined = TRUE;
726 version:
727 aNUM { $$ = MAKELONG($1, 0); }
728 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
733 static attr_t *make_attr(enum attr_type type)
735 attr_t *a = xmalloc(sizeof(attr_t));
736 a->type = type;
737 a->u.ival = 0;
738 INIT_LINK(a);
739 return a;
742 static attr_t *make_attrv(enum attr_type type, unsigned long val)
744 attr_t *a = xmalloc(sizeof(attr_t));
745 a->type = type;
746 a->u.ival = val;
747 INIT_LINK(a);
748 return a;
751 static attr_t *make_attrp(enum attr_type type, void *val)
753 attr_t *a = xmalloc(sizeof(attr_t));
754 a->type = type;
755 a->u.pval = val;
756 INIT_LINK(a);
757 return a;
760 static expr_t *make_expr(enum expr_type type)
762 expr_t *e = xmalloc(sizeof(expr_t));
763 e->type = type;
764 e->ref = NULL;
765 e->u.lval = 0;
766 e->is_const = FALSE;
767 INIT_LINK(e);
768 return e;
771 static expr_t *make_exprl(enum expr_type type, long val)
773 expr_t *e = xmalloc(sizeof(expr_t));
774 e->type = type;
775 e->ref = NULL;
776 e->u.lval = val;
777 e->is_const = FALSE;
778 INIT_LINK(e);
779 /* check for numeric constant */
780 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
781 e->is_const = TRUE;
782 e->cval = val;
784 return e;
787 static expr_t *make_exprs(enum expr_type type, char *val)
789 expr_t *e;
790 e = xmalloc(sizeof(expr_t));
791 e->type = type;
792 e->ref = NULL;
793 e->u.sval = val;
794 e->is_const = FALSE;
795 INIT_LINK(e);
796 /* check for predefined constants */
797 if (type == EXPR_IDENTIFIER) {
798 var_t *c = find_const(val, 0);
799 if (c) {
800 e->u.sval = c->name;
801 free(val);
802 e->is_const = TRUE;
803 e->cval = c->lval;
806 return e;
809 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
811 expr_t *e;
812 e = xmalloc(sizeof(expr_t));
813 e->type = type;
814 e->ref = expr;
815 e->u.tref = tref;
816 e->is_const = FALSE;
817 INIT_LINK(e);
818 /* check for cast of constant expression */
819 if (type == EXPR_CAST && expr->is_const) {
820 e->is_const = TRUE;
821 e->cval = expr->cval;
823 return e;
826 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
828 expr_t *e;
829 e = xmalloc(sizeof(expr_t));
830 e->type = type;
831 e->ref = expr;
832 e->u.lval = 0;
833 e->is_const = FALSE;
834 INIT_LINK(e);
835 /* check for compile-time optimization */
836 if (expr->is_const) {
837 e->is_const = TRUE;
838 switch (type) {
839 case EXPR_NEG:
840 e->cval = -expr->cval;
841 break;
842 case EXPR_NOT:
843 e->cval = ~expr->cval;
844 break;
845 default:
846 e->is_const = FALSE;
847 break;
850 return e;
853 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
855 expr_t *e;
856 e = xmalloc(sizeof(expr_t));
857 e->type = type;
858 e->ref = expr1;
859 e->u.ext = expr2;
860 e->is_const = FALSE;
861 INIT_LINK(e);
862 /* check for compile-time optimization */
863 if (expr1->is_const && expr2->is_const) {
864 e->is_const = TRUE;
865 switch (type) {
866 case EXPR_ADD:
867 e->cval = expr1->cval + expr2->cval;
868 break;
869 case EXPR_SUB:
870 e->cval = expr1->cval - expr2->cval;
871 break;
872 case EXPR_MUL:
873 e->cval = expr1->cval * expr2->cval;
874 break;
875 case EXPR_DIV:
876 e->cval = expr1->cval / expr2->cval;
877 break;
878 case EXPR_OR:
879 e->cval = expr1->cval | expr2->cval;
880 break;
881 case EXPR_AND:
882 e->cval = expr1->cval & expr2->cval;
883 break;
884 case EXPR_SHL:
885 e->cval = expr1->cval << expr2->cval;
886 break;
887 case EXPR_SHR:
888 e->cval = expr1->cval >> expr2->cval;
889 break;
890 default:
891 e->is_const = FALSE;
892 break;
895 return e;
898 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
900 expr_t *e;
901 e = xmalloc(sizeof(expr_t));
902 e->type = type;
903 e->ref = expr1;
904 e->u.ext = expr2;
905 e->ext2 = expr3;
906 e->is_const = FALSE;
907 INIT_LINK(e);
908 /* check for compile-time optimization */
909 if (expr1->is_const && expr2->is_const && expr3->is_const) {
910 e->is_const = TRUE;
911 switch (type) {
912 case EXPR_COND:
913 e->cval = expr1->cval ? expr2->cval : expr3->cval;
914 break;
915 default:
916 e->is_const = FALSE;
917 break;
920 return e;
923 static type_t *make_type(unsigned char type, type_t *ref)
925 type_t *t = xmalloc(sizeof(type_t));
926 t->name = NULL;
927 t->type = type;
928 t->ref = ref;
929 t->rname = NULL;
930 t->attrs = NULL;
931 t->funcs = NULL;
932 t->fields = NULL;
933 t->ignore = parse_only;
934 t->is_const = FALSE;
935 t->sign = 0;
936 t->defined = FALSE;
937 t->written = FALSE;
938 INIT_LINK(t);
939 return t;
942 static typeref_t *make_tref(char *name, type_t *ref)
944 typeref_t *t = xmalloc(sizeof(typeref_t));
945 t->name = name;
946 t->ref = ref;
947 t->uniq = ref ? 0 : 1;
948 return t;
951 static typeref_t *uniq_tref(typeref_t *ref)
953 typeref_t *t = ref;
954 type_t *tp;
955 if (t->uniq) return t;
956 tp = make_type(0, t->ref);
957 tp->name = t->name;
958 t->name = NULL;
959 t->ref = tp;
960 t->uniq = 1;
961 return t;
964 static type_t *type_ref(typeref_t *ref)
966 type_t *t = ref->ref;
967 if (ref->name) free(ref->name);
968 free(ref);
969 return t;
972 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
974 v->type = ref->ref;
975 v->tname = ref->name;
976 ref->name = NULL;
977 free(ref);
978 v->array = arr;
981 static ifref_t *make_ifref(type_t *iface)
983 ifref_t *l = xmalloc(sizeof(ifref_t));
984 l->iface = iface;
985 l->attrs = NULL;
986 INIT_LINK(l);
987 return l;
990 static var_t *make_var(char *name)
992 var_t *v = xmalloc(sizeof(var_t));
993 v->name = name;
994 v->ptr_level = 0;
995 v->type = NULL;
996 v->tname = NULL;
997 v->attrs = NULL;
998 v->array = NULL;
999 v->eval = NULL;
1000 v->lval = 0;
1001 INIT_LINK(v);
1002 return v;
1005 static func_t *make_func(var_t *def, var_t *args)
1007 func_t *f = xmalloc(sizeof(func_t));
1008 f->def = def;
1009 f->args = args;
1010 f->ignore = parse_only;
1011 f->idx = -1;
1012 INIT_LINK(f);
1013 return f;
1016 static class_t *make_class(char *name)
1018 class_t *c = xmalloc(sizeof(class_t));
1019 c->name = name;
1020 c->attrs = NULL;
1021 c->ifaces = NULL;
1022 INIT_LINK(c);
1023 return c;
1026 #define HASHMAX 64
1028 static int hash_ident(const char *name)
1030 const char *p = name;
1031 int sum = 0;
1032 /* a simple sum hash is probably good enough */
1033 while (*p) {
1034 sum += *p;
1035 p++;
1037 return sum & (HASHMAX-1);
1040 /***** type repository *****/
1042 struct rtype {
1043 char *name;
1044 type_t *type;
1045 int t;
1046 struct rtype *next;
1049 struct rtype *type_hash[HASHMAX];
1051 static type_t *reg_type(type_t *type, char *name, int t)
1053 struct rtype *nt;
1054 int hash;
1055 if (!name) {
1056 yyerror("registering named type without name\n");
1057 return type;
1059 hash = hash_ident(name);
1060 nt = xmalloc(sizeof(struct rtype));
1061 nt->name = name;
1062 nt->type = type;
1063 nt->t = t;
1064 nt->next = type_hash[hash];
1065 type_hash[hash] = nt;
1066 return type;
1069 /* determine pointer type from attrs */
1070 static unsigned char get_pointer_type( type_t *type )
1072 int t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1073 if( t ) return t;
1074 return RPC_FC_FP;
1077 static type_t *reg_types(type_t *type, var_t *names, int t)
1079 type_t *ptr = type;
1080 int ptrc = 0;
1082 while (names) {
1083 var_t *next = NEXT_LINK(names);
1084 if (names->name) {
1085 type_t *cur = ptr;
1086 int cptr = names->ptr_level;
1087 if (cptr > ptrc) {
1088 while (cptr > ptrc) {
1089 int t = get_pointer_type( cur );
1090 cur = ptr = make_type(t, cur);
1091 ptrc++;
1093 } else {
1094 while (cptr < ptrc) {
1095 cur = cur->ref;
1096 cptr++;
1099 reg_type(cur, names->name, t);
1101 free(names);
1102 names = next;
1104 return type;
1107 static type_t *find_type(char *name, int t)
1109 struct rtype *cur = type_hash[hash_ident(name)];
1110 while (cur && (cur->t != t || strcmp(cur->name, name)))
1111 cur = cur->next;
1112 if (!cur) {
1113 yyerror("type %s not found\n", name);
1114 return NULL;
1116 return cur->type;
1119 static type_t *find_type2(char *name, int t)
1121 type_t *tp = find_type(name, t);
1122 free(name);
1123 return tp;
1126 int is_type(const char *name)
1128 struct rtype *cur = type_hash[hash_ident(name)];
1129 while (cur && (cur->t || strcmp(cur->name, name)))
1130 cur = cur->next;
1131 if (cur) return TRUE;
1132 return FALSE;
1135 static type_t *get_type(unsigned char type, char *name, int t)
1137 struct rtype *cur = NULL;
1138 type_t *tp;
1139 if (name) {
1140 cur = type_hash[hash_ident(name)];
1141 while (cur && (cur->t != t || strcmp(cur->name, name)))
1142 cur = cur->next;
1144 if (cur) {
1145 free(name);
1146 return cur->type;
1148 tp = make_type(type, NULL);
1149 tp->name = name;
1150 if (!name) return tp;
1151 return reg_type(tp, name, t);
1154 static type_t *get_typev(unsigned char type, var_t *name, int t)
1156 char *sname = NULL;
1157 if (name) {
1158 sname = name->name;
1159 free(name);
1161 return get_type(type, sname, t);
1164 static int get_struct_type(var_t *field)
1166 int has_pointer = 0;
1167 int has_conformant_array = 0;
1168 int has_conformant_string = 0;
1170 while (field)
1172 type_t *t = field->type;
1174 /* get the base type */
1175 while( (t->type == 0) && t->ref )
1176 t = t->ref;
1178 switch (t->type)
1181 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1182 * Simple types don't effect the type of struct.
1183 * A struct containing a simple struct is still a simple struct.
1184 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1186 case 0: /* void pointer */
1187 case RPC_FC_BYTE:
1188 case RPC_FC_CHAR:
1189 case RPC_FC_SMALL:
1190 case RPC_FC_USMALL:
1191 case RPC_FC_WCHAR:
1192 case RPC_FC_SHORT:
1193 case RPC_FC_USHORT:
1194 case RPC_FC_LONG:
1195 case RPC_FC_ULONG:
1196 case RPC_FC_INT3264:
1197 case RPC_FC_UINT3264:
1198 case RPC_FC_HYPER:
1199 case RPC_FC_FLOAT:
1200 case RPC_FC_DOUBLE:
1201 case RPC_FC_STRUCT:
1202 case RPC_FC_ENUM16:
1203 case RPC_FC_ENUM32:
1204 break;
1206 case RPC_FC_UP:
1207 case RPC_FC_FP:
1208 has_pointer = 1;
1209 break;
1210 case RPC_FC_CARRAY:
1211 has_conformant_array = 1;
1212 break;
1213 case RPC_FC_C_CSTRING:
1214 case RPC_FC_C_WSTRING:
1215 has_conformant_string = 1;
1216 break;
1219 * Propagate member attributes
1220 * a struct should be at least as complex as its member
1222 case RPC_FC_CVSTRUCT:
1223 has_conformant_string = 1;
1224 has_pointer = 1;
1225 break;
1227 case RPC_FC_CPSTRUCT:
1228 has_conformant_array = 1;
1229 has_pointer = 1;
1230 break;
1232 case RPC_FC_CSTRUCT:
1233 has_conformant_array = 1;
1234 break;
1236 case RPC_FC_PSTRUCT:
1237 has_pointer = 1;
1238 break;
1240 default:
1241 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1242 field->name, t->type);
1243 /* fallthru - treat it as complex */
1245 /* as soon as we see one of these these members, it's bogus... */
1246 case RPC_FC_IP:
1247 case RPC_FC_ENCAPSULATED_UNION:
1248 case RPC_FC_NON_ENCAPSULATED_UNION:
1249 case RPC_FC_TRANSMIT_AS:
1250 case RPC_FC_REPRESENT_AS:
1251 case RPC_FC_PAD:
1252 case RPC_FC_EMBEDDED_COMPLEX:
1253 case RPC_FC_BOGUS_STRUCT:
1254 return RPC_FC_BOGUS_STRUCT;
1256 field = NEXT_LINK(field);
1259 if( has_conformant_string && has_pointer )
1260 return RPC_FC_CVSTRUCT;
1261 if( has_conformant_array && has_pointer )
1262 return RPC_FC_CPSTRUCT;
1263 if( has_conformant_array )
1264 return RPC_FC_CSTRUCT;
1265 if( has_pointer )
1266 return RPC_FC_PSTRUCT;
1267 return RPC_FC_STRUCT;
1270 /***** constant repository *****/
1272 struct rconst {
1273 char *name;
1274 var_t *var;
1275 struct rconst *next;
1278 struct rconst *const_hash[HASHMAX];
1280 static var_t *reg_const(var_t *var)
1282 struct rconst *nc;
1283 int hash;
1284 if (!var->name) {
1285 yyerror("registering constant without name\n");
1286 return var;
1288 hash = hash_ident(var->name);
1289 nc = xmalloc(sizeof(struct rconst));
1290 nc->name = var->name;
1291 nc->var = var;
1292 nc->next = const_hash[hash];
1293 const_hash[hash] = nc;
1294 return var;
1297 static var_t *find_const(char *name, int f)
1299 struct rconst *cur = const_hash[hash_ident(name)];
1300 while (cur && strcmp(cur->name, name))
1301 cur = cur->next;
1302 if (!cur) {
1303 if (f) yyerror("constant %s not found\n", name);
1304 return NULL;
1306 return cur->var;